getElementsByTagName problem

I have a javascript function which is passed a CheckBoxList and should find all "input" tags within it so I can check if boxes are checked or not.


However chkitm.length is always zero even though there are five "input" tags in view source. See attached text file.

I also tried childnodes but this does not work either. I am using IE9.

Anyone know why this is not working and have suggestions what I can do?
Source.txt
function.txt
LVL 2
Paulmc999Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
guru_samiConnect With a Mentor Commented:
is your script in the page itself or in a separate .js file? If separate js file it could be the browser caching.
0
 
leakim971PluritechnicianCommented:
work fine for me : http://jsfiddle.net/HPSmG/
0
 
masterpassCommented:
The script is just fine and it works unless "cblItm" is null. Try alerting the cblItm object and see what output you get.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Paulmc999Author Commented:
when i put a breakpoint cblItm is not null, but chkitem.length is zero

however i added an alert but alert never displays, yet function is working because it makes error visible

its very strange, i am using VS 2010
0
 
leakim971PluritechnicianCommented:
I think you're using a bad name reference, so cblItm is a goog string but this is not the ID of the div(or any control it is)

do a right on your page, locate the div cblItm and check its ID attribute
if you need more help, please provide a link to your page or post what you get viewing the source of it.

http://www.jagregory.com/writings/how-to-use-clientids-in-javascript-without-the-ugliness/
http://www.4guysfromrolla.com/articles/031710-1.aspx
0
 
Paulmc999Author Commented:
You can see the span ID in my view source code??
0
 
leakim971PluritechnicianCommented:
I want the one from your web browser not your source code...

do a right on your page, choose view source and post it here, thanks
0
 
Paulmc999Author Commented:
THAT IS from my view source
0
 
guru_samiCommented:
I think something going on due to asp.net renaming your checkboxlist.
How is your CheckListValidation being called i.e. what is cblItm?
0
 
leakim971PluritechnicianCommented:
THAT IS from my view source

I don't see where you're calling the function
0
 
Paulmc999Author Commented:
Again you can see it in the VIEW SOURCE
0
 
leakim971PluritechnicianCommented:
I don't see any call to CheckListValidation here :
 <span id="MainContent_Wizard1_CBL_2">
<input id="MainContent_Wizard1_CBL_2_0" type="checkbox" name="ctl00$MainContent$Wizard1$CBL_2$0" onclick="javascript:setTimeout(&#39;__doPostBack(\&#39;ctl00$MainContent$Wizard1$CBL_2$0\&#39;,\&#39;\&#39;)&#39;, 0)" value="One" /><label for="MainContent_Wizard1_CBL_2_0">One</label>
<input id="MainContent_Wizard1_CBL_2_2" type="checkbox" name="ctl00$MainContent$Wizard1$CBL_2$2" onclick="javascript:setTimeout(&#39;__doPostBack(\&#39;ctl00$MainContent$Wizard1$CBL_2$2\&#39;,\&#39;\&#39;)&#39;, 0)" value="Two" /><label for="MainContent_Wizard1_CBL_2_2">Two</label>
<input id="MainContent_Wizard1_CBL_2_4" type="checkbox" name="ctl00$MainContent$Wizard1$CBL_2$4" onclick="javascript:setTimeout(&#39;__doPostBack(\&#39;ctl00$MainContent$Wizard1$CBL_2$4\&#39;,\&#39;\&#39;)&#39;, 0)" value="Three" /><label for="MainContent_Wizard1_CBL_2_4">Three</label><br />
<input id="MainContent_Wizard1_CBL_2_1" type="checkbox" name="ctl00$MainContent$Wizard1$CBL_2$1" onclick="javascript:setTimeout(&#39;__doPostBack(\&#39;ctl00$MainContent$Wizard1$CBL_2$1\&#39;,\&#39;\&#39;)&#39;, 0)" value="Four" /><label for="MainContent_Wizard1_CBL_2_1">Four</label>
<input id="MainContent_Wizard1_CBL_2_3" type="checkbox" name="ctl00$MainContent$Wizard1$CBL_2$3" onclick="javascript:setTimeout(&#39;__doPostBack(\&#39;ctl00$MainContent$Wizard1$CBL_2$3\&#39;,\&#39;\&#39;)&#39;, 0)" value="Five" /><label for="MainContent_Wizard1_CBL_2_3">Five</label>
</span>

Open in new window

0
 
Paulmc999Author Commented:
Here it is if you think it helps:

<input type="submit" name="ctl00$MainContent$Wizard1$AddBtn_2" value="Add" onclick="return CheckListValidation(&#39;MainContent_Wizard1_CBL_2&#39;,&#39;cblError1&#39;);WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$MainContent$Wizard1$AddBtn_2&quot;, &quot;&quot;, true, &quot;check&quot;, &quot;&quot;, false, false))" id="MainContent_Wizard1_AddBtn_2" />
0
 
guru_samiCommented:
@Paulmc999,
What leakim971 and I want to know is how and when exactly your CheckListValidation  function is called?
Is it when you click a button or if it's autopostback?
Would like to see code from your .aspx page and not from browser viewsource.
0
 
Paulmc999Author Commented:
You cannot see what asp.net is naming things without looking in the browser source so I'm a bit confused.

However here is the aspx code:

<asp:Button ID="AddBtn_2" runat="server" Text="Add" OnClick="AddBtn_Click" ValidationGroup="check" OnClientClick="" />


onClientClick is set in the code behind file:

AddB.OnClientClick = "return CheckListValidation('MainContent_Wizard1_CBL_"+ctrl+"','cblError1');";



where ctrl is 2
0
 
leakim971PluritechnicianCommented:
thank you, why don't we see this button in your browser page source?
0
 
leakim971PluritechnicianCommented:
Here it is if you think it helps:

I don't think it's gonne to help but I think this is your main issue
0
 
Paulmc999Author Commented:
Because the browser page source is very big and i didnt post it all, do you think the function is not being called? it definitely is because the error is always displayed.
0
 
leakim971Connect With a Mentor PluritechnicianCommented:
do you think the function is not being called? it definitely is because the error is always displayed.

never say that...

if it's very long, could you post the code of the button? thanks
0
 
Paulmc999Author Commented:
never say what?? the error can only be displayed if the function is called plus when i debug it goes into the function???


here is the view source of the button:

<input type="submit" name="ctl00$MainContent$Wizard1$AddBtn_2" value="Add" onclick="return CheckListValidation(&#39;MainContent_Wizard1_CBL_2&#39;,&#39;cblError1&#39;);WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$MainContent$Wizard1$AddBtn_2&quot;, &quot;&quot;, true, &quot;check&quot;, &quot;&quot;, false, false))" id="MainContent_Wizard1_AddBtn_2" />
0
 
leakim971PluritechnicianCommented:
never say what??
<< do you think the function is not being called? it definitely is because the error is always displayed >>
0
 
Paulmc999Author Commented:
@leakim971

this is very cryptic, why should i never say that???
0
 
masterpassCommented:
What happens if you do this ?
var tDiv = document.getElementById('MainContent_Wizard1_CBL_2');
alert(tDiv.id);

Open in new window


and of course, do the rest of the code
0
 
Paulmc999Author Commented:
Well it worked perfectly, so then I changed it back to

var tDiv = document.getElementById(cblItm);
alert(tDiv.id);

and it now works too!!!!

any explanations for that guys??

i mean we haven't changed any of the code and yet now it suddenly works?
0
 
guru_samiCommented:
no ideas...just make sure it's the new script that's been run and not the old one.
0
 
masterpassCommented:
if you use something like a TFS. check in the code that curently works and if you had checked in earlier. try spotting the difference in code, if any. other than that, I have no idea as to why it happened.
0
 
leakim971PluritechnicianCommented:
this is very cryptic, why should i never say that???
forget it
0
 
Paulmc999Author Commented:
dont use TFS; yes definitely right script cause it started to work as soon as I made masterpass changes, which didnt change the code but made it work. Has to be something to do with the way VS 2010 compiles the code, its almost like it wasn't recognizing changes in function but suddenly now it did.

For example even though I have removed the alert I added, it has not disappeared, so im still trying to figure out what forces VS2010 to recognize script changes.
0
 
masterpassConnect With a Mentor Commented:
May be the browser is caching the page and still using the old script. try something like private browsing or incognito mode in browsers and see if it helps
0
 
Paulmc999Connect With a Mentor Author Commented:
ok guys apparently it was a simple matter of clearing the browser cache
0
 
Paulmc999Author Commented:
yes gura_sami that was it exactly, my comment crossed with yours lol
0
 
Sar1973Commented:
Try this code:
function myCheckBox() {
	
	var myControl = document.getElementsByTagName("input");
	var myCheck=0; 
	for (var i=0; i<myControl.length; i++) {
		if (myControlI[i].getAttribute("type") == "checkbox") {
			if (myControlI[i].checked==true) }
				myCheck++;
			}
		}
	}
	if (myCheck>0) }
		alert("At least 1 check box checked");
	}

}

Open in new window

If you want to create an array with every 0-1 value, you must develop the function obviously.
0
 
Paulmc999Author Commented:
Thanks guys
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.