?
Solved

Loop to find ul tag with correct id

Posted on 2012-08-10
1
Medium Priority
?
423 Views
Last Modified: 2012-08-11
I'm working on building a script that will loop through all ul's on the page and grab the one with the correct id, but am having slight difficulties.  Any thoughts:

var ul = document.getElementsByTagName("ul");
var polite = ["input1", "input2", "input3"];
for(var u=0; u<ul.length; u++) {
	for(var p=0; p<ul.attributes.length; p++) {
		if(ul[p].getAttribute("id") == polite[p]) {
			polite = ul[p];
		}
	}
}
console.log(polite);
var politeLoads = polite.getElementsByTagName("input");

Open in new window

0
Comment
Question by:pingeyeg
1 Comment
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 38281721
ids are supposed to be unique throughout the document, so just loop through your list of ids. Try:
var polite = ["input1", "input2", "input3"];
var i=-1,limit=polite.length;
while(++i<limit)
{
	var element=document.getElementById(polite[i]);
	if(element)
	{
		alert("Found " + polite[i] + ": <" +element.tagName +">" );
	}
}

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question