Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I need help with Javascript code. What i want to do basically is to loop through all controls in the page. The page contains table, gridview with itemtemplates etc. I want to set certain value for element type textbox and another value for elementtype label.
my code looks something like this:
JavaScript
function postExecute() {
       for (var i = 0; i < document.forms[0].elements.length; i++) {
           element = document.forms[0].elements[i];

           switch (element.type) {
               case 'text':
                   if (element.value.length > 0) {
                       element.value =  element.value.replace("<", '<'); //do htmlDecode
                   }
                   break;
               case 'span':
                   if (element.value.length > 0) {
                       element.value = element.value.replace("<", '<');
                       //do htmlcode.
                   }
                   break;
           }
       }

   }



If I put only for elementtype text. It works. I want it to work for elementtype label. not all labels have a span. Is there an elementtype "label" in javascript?? or what would be a way to get this done??
Posted
Updated 17-Aug-13 22:46pm
v2

Here's a sample using jQuery to locate all Textbox elements

JavaScript
$('form input[type="text"]').each(function(){
        // Do stuff with each of the Textboxes here e.g.:
        if (this.value.match(/\D/)) // regular expression for numbers only.
            Foo();
});



* To learn more about jQuery[^]

Cheers,
Edo
 
Share this answer
 
v4
all element has tag name. So you can get any element by tagname.
such as you want to get all the element with tag INPUT then use getelementsbytagname function.

You are looking for TYPE attribute. TYPE attribute is only available for element INPUT

SPAN is a ELEMENT. It does not have any attribute name TYPE. Now if you want to know the type of element then use tagName. i.e. element.tagName. samething about Label. You do not have TYPE attribute. But using tagName you can figure out what type of element are you dealing with.
 
Share this answer
 
Comments
namrata.cfa1 19-Aug-13 14:22pm    
getelementsbytagname worked. however, there is another problem now. I want the innerHTML to be replaced with something.
For example: the text displayed in the label is "abc<pqr">
When I do labels[i].innerHTML = labels[i].innerHTML.replace(/ get the following output: "abc< span="">
Mohibur Rashid 19-Aug-13 23:07pm    
either you can go for regular expression or you can use a id to retrieve data from the span

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900