Jump to content

getElementsByName is leading to unresponsive nature


tamanna35

Recommended Posts

Hello I am using something like thisvar location = document.getElementsByName("Location"); var cnt = 0; while( cnt < location.length ) { if (location[cnt].checked == false) { document.getElementsByName("Location")[cnt].value=""; cnt++ } }but this is leading to unresponsive behaviouslocation is the name for checkboxes in page like <input type="Checkbox" name="Location" value="New Jersey"> <input type="Checkbox" name="Location" value="Mexico"> <input type="Checkbox" name="Location" value="Texas"> <input type="Checkbox" name="Location" value="Singapore">etcthe page is returning XML<Location > NewJersey, Mexico, Singapore </Location>only if New Jersey and Mexico checkboxes are checkedI want the XML As <Location > NewJersey, Mexico, , Singapore</Location>that is... if the checkbox is not checked, it should return an empty string .and that is why I am using getelementsbynameI also triedvar location = document.getElementsByName("Location"); var cnt = 0; while( cnt < location.length ) { if (location[cnt].checked == false) { location[cnt].value=""; cnt++ } }but that also gives the same message,"A script on this page is causing Internet Explorer to run slowly................."How this can be acheived?thank you very much

Link to comment
Share on other sites

so you're trying to build an XML document based on the inputs checked?Did you alert location and maybe make a quick loop to see that you are indeed getting the correct inputs and their values? Something like this:

for(i = 0, length = location.length; i < length; i++){  alert("Location " + (i + 1) + ": " + location[i].value);};

Link to comment
Share on other sites

so you're trying to build an XML document based on the inputs checked?Did you alert location and maybe make a quick loop to see that you are indeed getting the correct inputs and their values? Something like this:
for(i = 0, length = location.length; i < length; i++){  alert("Location " + (i + 1) + ": " + location[i].value);};

yes, I had put a break point and the array is able to get the whole list of 10 location that I have put as a checkbox option, I can click on them and see all their properties.I can see the "checked" property as false for the one that were not checked, and that's why I thought "if (location[cnt].checked == false)"will work.
Link to comment
Share on other sites

the while loop will never end, because cnt++ is within the if conditiontrywhile( cnt < location.length ) { if (location[cnt].checked == false) { document.getElementsByName("Location")[cnt].value=""; //cnt++; }cnt++ }
exactly, I removed the cnt outside and its back to working, BUT not the way I expected, its still giving me the list of the checked locations, whereas I am expecting an empty string for the location that has not been checked.that is something like<Location > NewJersey, Mexico, , Singapore , , , Hawaii</Location>(those empty commas show that those locations were not checkedBut I am actually still getting this.<Location > NewJersey, Mexico, Singapore ,Hawaii</Location>why thisif (location[cnt].checked == false) { document.getElementsByName("Location")[cnt].value=""; }fails to bring the desired effect,I thought this will make the value of those which has not been checked = "".but no.Can someone help me with this part too. Thank you very much
Link to comment
Share on other sites

I'm not familiar with XML or how to generate it, but is this being submitted to a server side script? If so, a checkbox that's not checked is not submitted. So if you have the following checkbox:<input type='checkbox' name='chkOne' value='test' />and it's not checked when you submit, then $_POST['chkOne']** and $_GET['chkOne']** will not exist.Edit:**Or whichever variables in ASP or other languages hold the POST and GET values. :)

Link to comment
Share on other sites

I'm not familiar with XML or how to generate it, but is this being submitted to a server side script? If so, a checkbox that's not checked is not submitted. So if you have the following checkbox:<input type='checkbox' name='chkOne' value='test' />and it's not checked when you submit, then $_POST['chkOne']** and $_GET['chkOne']** will not exist.Edit:**Or whichever variables in ASP or other languages hold the POST and GET values. :)
you are right so I did this and I get it to workif (location[cnt].checked == false) { document.getElementsByName("Location")[cnt].checked=true; document.getElementsByName("Location")[cnt].value=" "; }Thank you so very much everyone
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...