Link to home
Start Free TrialLog in
Avatar of Paulmc999
Paulmc999

asked on

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
Avatar of leakim971
leakim971
Flag of Guadeloupe image

work fine for me : http://jsfiddle.net/HPSmG/
The script is just fine and it works unless "cblItm" is null. Try alerting the cblItm object and see what output you get.
Avatar of Paulmc999
Paulmc999

ASKER

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
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
You can see the span ID in my view source code??
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
THAT IS from my view source
I think something going on due to asp.net renaming your checkboxlist.
How is your CheckListValidation being called i.e. what is cblItm?
THAT IS from my view source

I don't see where you're calling the function
Again you can see it in the VIEW SOURCE
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

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" />
@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.
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
thank you, why don't we see this button in your browser page source?
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
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.
SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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" />
never say what??
<< do you think the function is not being called? it definitely is because the error is always displayed >>
@leakim971

this is very cryptic, why should i never say that???
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
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?
no ideas...just make sure it's the new script that's been run and not the old one.
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.
this is very cryptic, why should i never say that???
forget it
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yes gura_sami that was it exactly, my comment crossed with yours lol
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.
Thanks guys