Jump to content

checkbox trouble..


astralaaron

Recommended Posts

Will someone please explain why example 1 works and example 2 doesnt?/////// EXAMPLE 1 /////////////function testing(){ alert(document.pwizreturn.pwztest2.checked);}<input type="checkbox" name="test2" value="2" onclick="testing();" />////////////////////////////////////////// EXAMPLE 2 //////////////////function testing(testElm){ alert(document.pwizreturn.testElm.checked);}<input type="checkbox" name="test2" value="2" onclick="testing('test2');" />/////////////////////////////////////////example 1 will say true or false, and example 2 nothing happens. As if the checkbox doesnt exist...I tried a text input field in example 2 passing the name through as a parameter and it worked fine. I can't seem to figure out why its not working with check boxes.

Link to comment
Share on other sites

Because it's taking testElm as a literal property name, instead of interpreting the string. Think about it:

var a = {"b" : "x", "c" : "y"};var b = "c";alert(a.b); // "x"

You can use, uh, array notation instead if you want to use a variable reference.

Link to comment
Share on other sites

Ok I understand it's looking for the property name literally. I put the value into a new array and tried that as well and am still getting the same result. I don't think I understand what you mean by using array notation instead... I basically did:var a = new Array(testElm);alert(a[0]); // test2then tried alert(document.formname.a[0].checked);and got the same result, nothing..i'll keep fooling with it but please hint me in if you see my mistake!

Link to comment
Share on other sites

There's always a different number of check boxes so they have names that increment like a1, a2, a3 etc.... so I'm trying to pass the name through as a parameter of the function where a few things will happen. Just can't seem to get it to literally look for the name I pass through.

Link to comment
Share on other sites

pass this to the function. It will pass a reference to the element itself. From there you should be able to access all properties and values of the element.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...