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
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
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.
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
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
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
ASKER
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
do a right on your page, choose view source and post it here, thanks
ASKER
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?
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
ASKER
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('__doPostBack(\'ctl00$MainContent$Wizard1$CBL_2$0\',\'\')', 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('__doPostBack(\'ctl00$MainContent$Wizard1$CBL_2$2\',\'\')', 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('__doPostBack(\'ctl00$MainContent$Wizard1$CBL_2$4\',\'\')', 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('__doPostBack(\'ctl00$MainContent$Wizard1$CBL_2$1\',\'\')', 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('__doPostBack(\'ctl00$MainContent$Wizard1$CBL_2$3\',\'\')', 0)" value="Five" /><label for="MainContent_Wizard1_CBL_2_3">Five</label>
</span>
ASKER
Here it is if you think it helps:
<input type="submit" name="ctl00$MainContent$Wi zard1$AddB tn_2" value="Add" onclick="return CheckListValidation('M ainContent _Wizard1_C BL_2', 'cblEr ror1') ;WebForm_D oPostBackW ithOptions (new WebForm_PostBackOptions(&q uot;ctl00$ MainConten t$Wizard1$ AddBtn_2&q uot;, "", true, "check", "", false, false))" id="MainContent_Wizard1_Ad dBtn_2" />
<input type="submit" name="ctl00$MainContent$Wi
@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.
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.
ASKER
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('MainC ontent_Wiz ard1_CBL_" +ctrl+"',' cblError1' );";
where ctrl is 2
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('MainC
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
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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$Wi zard1$AddB tn_2" value="Add" onclick="return CheckListValidation('M ainContent _Wizard1_C BL_2', 'cblEr ror1') ;WebForm_D oPostBackW ithOptions (new WebForm_PostBackOptions(&q uot;ctl00$ MainConten t$Wizard1$ AddBtn_2&q uot;, "", true, "check", "", false, false))" id="MainContent_Wizard1_Ad dBtn_2" />
here is the view source of the button:
<input type="submit" name="ctl00$MainContent$Wi
never say what??
<< do you think the function is not being called? it definitely is because the error is always displayed >>
<< do you think the function is not being called? it definitely is because the error is always displayed >>
ASKER
@leakim971
this is very cryptic, why should i never say that???
this is very cryptic, why should i never say that???
What happens if you do this ?
and of course, do the rest of the code
var tDiv = document.getElementById('MainContent_Wizard1_CBL_2');
alert(tDiv.id);
and of course, do the rest of the code
ASKER
Well it worked perfectly, so then I changed it back to
var tDiv = document.getElementById(cb lItm);
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?
var tDiv = document.getElementById(cb
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
ASKER
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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");
}
}
If you want to create an array with every 0-1 value, you must develop the function obviously.
ASKER
Thanks guys