Solved

Javascript : Cycle Div IDs w/Prefix & Reset HTML

Posted on 2010-09-05
6
415 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to make a moving character 3 62
How to show span when clicked on? 10 24
asp.net mvc5 6 17
Add shadow behind div 5 16
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…
Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
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…

825 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