Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Javascript : Cycle Div IDs w/Prefix & Reset HTML

Posted on 2010-09-05
6
Medium Priority
?
434 Views
Last Modified: 2012-05-10
I need a simple snippet to cycle through all the DIVs in a document with the ID prefix of "VALIDATIONRESPONSE." and reset the internal HTML

So there could be 10 DIVs with unique names like "VALIDATIONRESPONSE.email_addr" and "VALIDATIONRESPONSE.first_name"

I need a JS snippet to cycle through the Document that resets the Internal HTML of each div to NULL (empty)

0
Comment
Question by:SMabs
  • 3
  • 3
6 Comments
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 33608439
Check this :

            var divs = document.getElementsByTagName("div");
            for(var i=0;i=0;
                  if(b) {
                        divs[i].innerHTML = "";
                  }
            }

Test page :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">
	window.onload= function() {
		var divs = document.getElementsByTagName("div");
		for(var i=0;i<divs.length;i++) {
			var b = divs[i].innerHTML.indexOf("VALIDATIONRESPONSE")>=0;
			if(b) {
				divs[i].innerHTML = "";
			}
		}
	}
</script>
</head>
<body>
<div>VALIDATIONRESPONSE.email_addr</div>
<div>VALIDATIONRESPONSE.first_name</div>
<div>VALIDATIONRESPONSE.email_addr1</div>
<div>VALIDATIONRESPONSE.first_name1</div>
<div>VALIDATIONRESPONSE.email_addr2</div>
<div>VALIDATIONRESPONSE.first_name2</div>
<div>ALIDATIONRESPONSE.email_addr3</div>
<div>VALIDATIONRESPONSE.first_name3</div>
<div>VALIDATIONRESPONSE.email_addr4</div>
<div>VALIDATIONRESPONSE.first_name4</div>
<div>VALIDATIONRESPONSE.email_addr5</div>
<div>ALIDATIONRESPONSE.first_name5</div>
<div>VALIDATIONRESPONSE.email_addr6</div>
<div>VALIDATIONRESPONSE.first_name6</div>
<div>VALIDATIONRESPONSE.email_addr7</div>
<div>ALIDATIONRESPONSE.first_name7</div>
<div>VALIDATIONRESPONSE.email_addr8</div>
<div>VALIDATIONRESPONSE.first_name8</div>
<div>ALIDATIONRESPONSE.email_addr9</div>
<div>VALIDATIONRESPONSE.first_name9</div>
</body>
</html>

Open in new window

0
 

Author Comment

by:SMabs
ID: 33608452
the ID of the DIVs is VALIDATIONRESPONSE.  not the content...


example:  <div id="VALIDATIONRESPONSE.email_addr"></div>

0
 

Author Comment

by:SMabs
ID: 33608458
Here, I just modified your code.. the basics were there.

switched out innerHTML for id

Thanks :)


            var divs = document.getElementsByTagName("div");
            for(var i=0;i<divs.length;i++) {
                  var b = divs[i].id.indexOf("VALIDATIONRESPONSE.")>=0;
                  if(b) {
                        divs[i].innerHTML = "";
                  }
            }
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 83

Expert Comment

by:leakim971
ID: 33608460
Test page :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">
	window.onload= function() {
		var divs = document.getElementsByTagName("div");
		for(var i=0;i<divs.length;i++) {
			var b = divs[i].id.indexOf("VALIDATIONRESPONSE")>=0;
			if(b) {
				divs[i].innerHTML = "";
			}
		}
	}
</script>
</head>
<body>
<div id=VALIDATIONRESPONSE.email_addr" >SOME TEXT</div>
<div id=VALIDATIONRESPONSE.first_name" >SOME TEXT</div>
<div id=VALIDATIONRESPONSE.email_addr1" >SOME TEXT</div>
<div id=VALIDATIONRESPONSE.first_name1" >SOME TEXT</div>
<div id=VALIDATIONRESPONSE.email_addr2" >SOME TEXT</div>
<div id=VALIDATIONRESPONSE.first_name2" >SOME TEXT</div>
<div id=ALIDATIONRESPONSE.email_addr3" >SOME TEXT</div>
<div id=VALIDATIONRESPONSE.first_name3" >SOME TEXT</div>
<div id=VALIDATIONRESPONSE.email_addr4" >SOME TEXT</div>
<div id=VALIDATIONRESPONSE.first_name4" >SOME TEXT</div>
<div id=VALIDATIONRESPONSE.email_addr5" >SOME TEXT</div>
<div id="ALIDATIONRESPONSE.first_name5" >SOME TEXT</div>
<div id=VALIDATIONRESPONSE.email_addr6" >SOME TEXT</div>
<div id=VALIDATIONRESPONSE.first_name6" >SOME TEXT</div>
<div id=VALIDATIONRESPONSE.email_addr7" >SOME TEXT</div>
<div id="ALIDATIONRESPONSE.first_name7" >SOME TEXT</div>
<div id=VALIDATIONRESPONSE.email_addr8" >SOME TEXT</div>
<div id=VALIDATIONRESPONSE.first_name8" >SOME TEXT</div>
<div id="ALIDATIONRESPONSE.email_addr9" >SOME TEXT</div>
<div id=VALIDATIONRESPONSE.first_name9" >SOME TEXT</div>
</body>
</html>

Open in new window

0
 
LVL 83

Expert Comment

by:leakim971
ID: 33608462
You're welcome! Thanks for the points!
0
 

Author Comment

by:SMabs
ID: 33608466
if you would like to take a stab at the other question I have open.. the SWITCH string comparison issue... I need it solved.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult 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

927 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