Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

.each only

Posted on 2013-01-06
2
Medium Priority
?
169 Views
Last Modified: 2013-01-06
I thought that .each would pull an array back of all matching, not children elements of matching as well.

What Im trying to do is alternate colours of visable rows, which I thought would be easy using jQuery and .each(), however my code seems to pick the child div elements instead of just the matching element:-

HTML:-
<div id="divTransactions">
      <div id="row56" class="clsTransactionRow">
            <div class="txtTransDate">2012-12-24</div>
            <div class="txtTransText">DB Payment</div>
            <div class="txtTransStatus">Cleared</div>
      </div>
      <div id="row57" class="clsTransactionRow">
            <div class="txtTransDate">2012-12-23</div>
            <div class="txtTransText">CC Payment</div>
            <div class="txtTransStatus">Held</div>
      </div>
      <div id="row58" class="clsTransactionRow">
            <div class="txtTransDate">2012-12-22</div>
            <div class="txtTransText">Standing Order</div>
            <div class="txtTransStatus">Disputed</div>
      </div>
<div>

Open in new window

CSS:-
#divTransactions {width:400px; }
.clsTransactionRow { width:400px; }
.clsTransactionRowEven { background-color: #FFFFFF; }
.clsTransactionRowOdd { background-color: #CCCCCC; }

.txtTransDate { width:133px; float:left;}
.txtTransText { width:133px; float:left; }
.txtTransStatus { width:133px; float:left; }

Open in new window

JQuery:-
var rowCount=0;
$("#divTransactions .clsTransactionRow :visible").each( function(_index, _value ) {
        $(this).removeClass('clsTransactionRowEven');
        $(this).removeClass('clsTransactionRowOdd');
        if (rowCount%2==0){
            $(this).addClass('clsTransactionRowEven');
        }
        else {
            $(this).addClass('clsTransactionRowOdd');
        }
        rowCount++;
        });

Open in new window


To show you what I mean, Ive put it on jsFiddle:-
http://jsfiddle.net/DEVdJ/

How can I just get the matching rows to return, and not all the child elements, so I can set the row colours correctly?
0
Comment
Question by:tonelm54
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 38748467
0
 

Author Closing Comment

by:tonelm54
ID: 38748493
Thank you, again :-)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question