2

I have a "mytable" which contains "mytd" inturn contains the "Myspan" i want to get the .I want to get the "myspan_1" in jquery .How do i do that.

Small try here

$("#mytable").find('td > #span').attr("value","myspan_+i"); 

where "i" is an incrementing value

I coud not get Span id .All iam trying "id" value of span tag

4
  • 2
    It is difficult to tell what you want. It would be helpful if you posted part of the HTML. Do you want to get or set the ID of each span. Is there one span per <tr>? per <td>? Commented Jul 26, 2010 at 0:47
  • I want to get the id of each Span .If the Span is "myspan_1" or "myspan_2" i want to get the 1 and 2 value Commented Jul 26, 2010 at 0:52
  • Ok, but how many spans are there that have an ID? Do you want the number from every single one? How do you want it? In an array? Providing your HTML would be very helpful. It would answer a lot of questions. Commented Jul 26, 2010 at 1:01
  • @Patrick- There are many spans for each Row .But i want the just Incremented value or i want the value just i(myspan_i). Commented Jul 26, 2010 at 1:04

1 Answer 1

5

try these

$("#mytable").find("td > span").attr("id"); 

this will get the id of the current span. if you want to assign an id to the span you can do this.

$("#mytable").find("td > span").attr("id", "myspan_"+i"); 

where i is an incremental variable.

furthermore, if you are adding id to each td > span you can do a for loop or $.each() in all td of #mytable

var i=0; $.each($("#mytable").find("td"), function(){ $(this).find("span").attr("id", "myspan_"+i); i++; }); 

Edit:

As I read your title i think I miss understood your question. But I think here's what you want. You want to get the value of the attribute Value in the element with id of #span actually if you are looking for an id in jquery, you can call it directly since id is always unique.

$("#span").attr("value", "myspan_"+i); 

since i is an incremental value, you need to put it outside the quotes.

Edit 2:

To add value to an attribute value

$("#mytable").find("td > #span").attr("value", "myspan_"+i); 

Edit 3:

Assuming your span id has an incremental myspan_1 which always starts at 1

var spanid = $("#mytable").find("td > span").attr("id", "myspan_"+i); var idvalue = spanid.split('_')[1]; 

this will get the 1 of the myspan_1

or you could just directly get the i since i is the number that you want

var spanid = $("#mytable").find("td > span").attr("id", "myspan_"+i); var idvalue = i; 
Sign up to request clarification or add additional context in comments.

4 Comments

I think the last quote in the second example needs to go (as in your 3rd example), i.e. ("id", "myspan_"+i);
Thanks a lot The first one worked out .So Iam Getting the Value "myspan_1" .where "1" is the Incremented value and i want to get the just incremented Value "1" or "2" .How do i do that
@AskSomeone, is my edit what you are trying to ask for? I'm somewhat confused a little bit.
Iam Getting the <span id> .Now i want only the Incremented Value 1 or 2 or 3

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.