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!

How to retreive UL id from Javascript? 2

Status
Not open for further replies.
Oct 11, 2006
300
US
Hi,

I have a UL element which is built dynamically from the DB. I have a unique id to each UL tag by building it through DB.

For instance,
Code:
<ul id="menu">
  <li>
  	<input type="radio" checked id="empid" name="empid" value="4655">
    <a href="#" class="a_style" onClick="s_Hide('4655', this)">TopMgr</a>
    	<ul id="4655" style="display: none">
    		<li>
    			<input type="radio" id="empid" name="empid" value="2109922">
    			<a href="#" class="a_style" onClick="s_Hide('2109922', this); return false;">Mgr1</a>
				<ul id="2109922" style="display: none">

I have assigned the ids the same as the id retireved by the DB - employee ID

Now how can I retrieve this value from the Javascript?

Thanks.
 
You could try something like this:
Code:
var ulCollection = document.getElementsByTagName('ul');
for (var loop=0, max=ulCollection.length; loop<max; loop++) {
  if (ulCollection[loop].id != '') alert(ulCollection[loop].id);
}
I would advise you to adjust the way you set IDs, though. You need to start an ID with an alpha character (not a number). Add a letter at the start of the ID if you need to.

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
A question for you -

getElemnetsByTagName - what does this do?
'ul' - does this refer to the <ul> tag within the HTML?

Where should be this function embedded in my show hide function?

Code:
function s_Hide(el){
	obj = document.getElementById(el).style;
	(obj.display == 'none')? obj.display = 'block' : obj.display = 'none';
	//get_cookie(Name);
}

Thanks.

 
document.getElementsByTagName(), as the method is named, retrieves all the elements by the tag name you specify, and puts them in an array. You won't use that with what you have so far.

You asked how to retrieve the employee ID with Javascript. Where do you want to retrieve it to? What do you want to do with it?

You also send a reference to the <a> to your s_Hide function using the this keyword, but don't have it listed in the arguments in the function itself. While it's acceptable to send arguments that the function doesn't use, I'm curious why it's there in the first place.

Don't worry about changing the format of your IDs. An element name that starts with a digit limits the way it can be referenced, but by no means prevents it being referenced.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top