Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I would like to get the text from the first and second TD which has the class user and id

<tr class="rowid">
    <td class="user">A</td>
    <td class="id">B</td>
</tr>

So first i want A and then B I tried

$(document).ready(function(){
 $(".rowid").each(function(index, value) { 
var test= $(.user).text();
alert(test);

But i get A twice.I want one value at a time. Any ideas what 'm doing wrong? Cheers

share|improve this question

3 Answers 3

up vote 0 down vote accepted

This piece of code is always pointing to A:

var test = $(".user").text()

instead use:

$(".rowid").find("td").each(function() {
  var test = $(this).text();
  alert(test);
}

this will iterate over the td elements and output the text.

To get each value:

$(".rowid").each(function() {
  var a = $(this).find(".user").text();
  var b = $(this).find(".id").text();
}
share|improve this answer
    
Thanks Russel I do i get "A" and "B" seperately. Chrs –  naijacoder Jan 19 '12 at 0:41
    
I have added a block of code so you can iterate over each tr element and return the user and id (A and B text) for each one. Is that what you are trying to do? –  Russell Jan 19 '12 at 1:47
    
Chrs Russell. Hope to be on top off Jquery soon –  naijacoder Jan 19 '12 at 4:16
    
No worries. :) Good luck! –  Russell Jan 19 '12 at 4:59

First selector ".rowid" selects all tags with class "rowid". At you markup example it's only one tr tag, so you will itterate by each function only through one element. Second selector ".user" gives you all tags with class "user", but if you want to take A and then B, will need to take all td tags of current tr container or select ".user" td and ".id" td seperatly as in example below.

$(document).ready(function(){ 
    rows = $(".rowid").each(function(index,value){
            user = $(value).children(".user").text();
            id = $(value).children(".id").text();
            alert(id+'\t'+user);
    });
});
share|improve this answer
    
IAfanasov thanks tried it but didn't work –  naijacoder Jan 19 '12 at 0:38
<table id ="table1">
    <th>UserName</th>
    <th>Password</th>
    <tr>
        <td id = "1">username</td>
        <td id = "2">12344</td>
    </tr>
</table>

$('#table1 tr').each(function (){
    var vara = $(this).find('#2').html();
    if(vara != null)
        alert($(this).find('#2').html());
});
share|improve this answer
    
Please add some explanation when answering, rather than just dropping a code fragment. This will make your answer much more useful. Thanks. –  SPDoctor Nov 27 '12 at 23:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.