views:

138

answers:

1

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

+1  A: 

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;
rob waminal
I think the last quote in the second example needs to go (as in your 3rd example), i.e. ("id", "myspan_"+i);
Tom Carver
@rob: 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
@AskSomeone, is my edit what you are trying to ask for? I'm somewhat confused a little bit.
rob waminal
@rob: Iam Getting the <span id> .Now i want only the Incremented Value 1 or 2 or 3
AskSomeone