Solved

Javascript : Cycle Div IDs w/Prefix & Reset HTML

Posted on 2010-09-05
6
408 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
Comment Utility
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
Comment Utility
the ID of the DIVs is VALIDATIONRESPONSE.  not the content...


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

0
 

Author Comment

by:SMabs
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 82

Expert Comment

by:leakim971
Comment Utility
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
Comment Utility
You're welcome! Thanks for the points!
0
 

Author Comment

by:SMabs
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

772 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

10 Experts available now in Live!

Get 1:1 Help Now