Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get LABEL ID that is dynamic generated at server

Status
Not open for further replies.

dev2006v2

Programmer
Joined
Feb 17, 2006
Messages
5
Location
US
Hello
I am a newbie to Javascript. I have a list of labels that are dynamic generated by server. I need to get the ID from the LABELS for AJAX. There are 50 labels per page.
The html looks like:

<LABEL ID="Label1" onclick="GetSelectedItem()">Item A</LABEL>
<LABEL ID="Label2" onclick="GetSelectedItem()">Item B</LABEL>
<LABEL ID="Label3" onclick="GetSelectedItem()">Item C</LABEL>
<LABEL ID="Label4" onclick="GetSelectedItem()">Item D</LABEL>
and 46 more html labels.

ID="Label1" - ID="Label50" are generated by the server. When the user clicks the fourth label, I need to be able to get "Label4" which need to be passed to the GetSelectedItem() function.
How do I do this??????
Thank you.

 
Hello Dan,
Thanks for the tip. When I incorporated it I get another error: "Microsoft JScript runtime error: Object required"

function GetSelectedItem()

{
var LotNo=document.getElementById(this).innerText;
.......
}

Help! Thanks.
 
This is what it meant?
[tt]
function GetSelectedItem(sid)

{
var LotNo=document.getElementById(sid).innerHTML;
//.......
}
[/tt]
and make sure it is calling with parameter this.id as BRPS shown. (Also, use innerHTML except you have something specific that you must use innerText.
 
I assumed that when you asked for the ID to be passed into the function, you knew how to pick up on it.

Tsuji's method is one way, but if all you want the ID for is to reference the element, you could dispense with the ID altogether, saving time:

Code:
function GetSelectedItem(el) {
    var LotNo = el.innerText;
}

...

onclick="GetSelectedItem(this);"

If you want this to work in any browser other than IE, you should replace "innerText" with something a bit more compliant, such as "innerHTML".

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top