Solved

Javascript : Cycle Div IDs w/Prefix & Reset HTML

Posted on 2010-09-05
6
410 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 82

Accepted Solution

by:
leakim971 earned 500 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 82

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 82

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

867 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now