Solved

getElementsByTagName problem

Posted on 2013-01-16
33
481 Views
Last Modified: 2013-01-21
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
0
Comment
Question by:Paulmc999
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 10
  • 4
  • +2
33 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 38784564
work fine for me : http://jsfiddle.net/HPSmG/
0
 
LVL 21

Expert Comment

by:masterpass
ID: 38784603
The script is just fine and it works unless "cblItm" is null. Try alerting the cblItm object and see what output you get.
0
 
LVL 2

Author Comment

by:Paulmc999
ID: 38784675
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 82

Expert Comment

by:leakim971
ID: 38784738
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
 
LVL 2

Author Comment

by:Paulmc999
ID: 38784773
You can see the span ID in my view source code??
0
 
LVL 82

Expert Comment

by:leakim971
ID: 38784782
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
 
LVL 2

Author Comment

by:Paulmc999
ID: 38784790
THAT IS from my view source
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 38784791
I think something going on due to asp.net renaming your checkboxlist.
How is your CheckListValidation being called i.e. what is cblItm?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 38784801
THAT IS from my view source

I don't see where you're calling the function
0
 
LVL 2

Author Comment

by:Paulmc999
ID: 38784804
Again you can see it in the VIEW SOURCE
0
 
LVL 82

Expert Comment

by:leakim971
ID: 38784818
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
 
LVL 2

Author Comment

by:Paulmc999
ID: 38784819
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
 
LVL 41

Expert Comment

by:guru_sami
ID: 38784820
@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
 
LVL 2

Author Comment

by:Paulmc999
ID: 38784853
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
 
LVL 82

Expert Comment

by:leakim971
ID: 38784869
thank you, why don't we see this button in your browser page source?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 38784873
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
 
LVL 2

Author Comment

by:Paulmc999
ID: 38784875
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
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 150 total points
ID: 38784884
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
 
LVL 2

Author Comment

by:Paulmc999
ID: 38784899
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
 
LVL 82

Expert Comment

by:leakim971
ID: 38784908
never say what??
<< do you think the function is not being called? it definitely is because the error is always displayed >>
0
 
LVL 2

Author Comment

by:Paulmc999
ID: 38784915
@leakim971

this is very cryptic, why should i never say that???
0
 
LVL 21

Expert Comment

by:masterpass
ID: 38784929
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
 
LVL 2

Author Comment

by:Paulmc999
ID: 38784944
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
 
LVL 41

Expert Comment

by:guru_sami
ID: 38784957
no ideas...just make sure it's the new script that's been run and not the old one.
0
 
LVL 21

Expert Comment

by:masterpass
ID: 38784967
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
 
LVL 82

Expert Comment

by:leakim971
ID: 38784980
this is very cryptic, why should i never say that???
forget it
0
 
LVL 2

Author Comment

by:Paulmc999
ID: 38785030
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
 
LVL 21

Assisted Solution

by:masterpass
masterpass earned 175 total points
ID: 38785047
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
 
LVL 41

Accepted Solution

by:
guru_sami earned 175 total points
ID: 38785053
is your script in the page itself or in a separate .js file? If separate js file it could be the browser caching.
0
 
LVL 2

Assisted Solution

by:Paulmc999
Paulmc999 earned 0 total points
ID: 38785065
ok guys apparently it was a simple matter of clearing the browser cache
0
 
LVL 2

Author Comment

by:Paulmc999
ID: 38785068
yes gura_sami that was it exactly, my comment crossed with yours lol
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 38787256
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
 
LVL 2

Author Closing Comment

by:Paulmc999
ID: 38800539
Thanks guys
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question