Forum Moderators: open

Message Too Old, No Replies

javascript sending boolean variable

         

sbrocks

5:27 pm on Oct 16, 2009 (gmt 0)

10+ Year Member



I cannot understand why this doesn't work, anybody see what I've done wrong?
I create a group of radio buttons, last button has a subset of radio buttons. The subset needs to be disabled until it's parent button is selected.
I'm sending a boolean variable to a javascript function to change the subgroup from disabled=true to disabled=false, or so... I thought that was what I was doing!

Here is the javascript-


<script type="text/javascript">
function enableUs(c, b)
{
var i=0;
var m=new Array();
m=document.getElementsByName(c, b);

for (i=0;i<m.length;i++)
{
alert("in loop and b is " + b)
m.item(i).disabled=b;
}
return true;
}
</script>

This is the code that calls it-


echo "<p>";
echo "<input type='radio' name='pos_button' value='$interpret' id='$interpret' onclick='return enableUs(\"lang_button\", \"false\")'/>";
echo "<label for='pos_button'><FONT face='Verdana,Helvetica' SIZE='3' COLOR='Blue'><B>Interpreter</B></FONT></label>";
echo "<p id='interpret' style='display:block; margin-left:6px'>";
foreach ($_LANGUAGE as $key => $id)
{
echo "<p>";
echo "<input type='radio' name='lang_button' value=$key id=$key disabled='true'/>";
echo "<label for='lang_button'><FONT face='Verdana,Helvetica' SIZE='3' COLOR='Blue'><B>".$id['title']."</B></FONT></label>";
}
echo "</p>";

Can anyone see what I'm missing? Please?
Thanks,
Sara

rainborick

5:40 pm on Oct 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It may not matter, but getElementsByName() only takes one argument - the element name. You're passing two, so there's a chance that some JavaScript interpreters could be seeing the wrong value.

sbrocks

5:45 pm on Oct 16, 2009 (gmt 0)

10+ Year Member



AK! how did that get there?
Thank you rainborick, I fixed the typo so the script now looks like this-

<script type="text/javascript">
function enableUs(c, b)
{
var i=0;
var m=new Array();
m=document.getElementsByName(c);

for (i=0;i<m.length;i++)
{
alert("in loop b is " + b)
m.item(i).disabled=b;

}
return true;
}
</script>


But it still does not enable the subset of buttons. What's weird is that it DOES work on a set of drop-downs that have the same name=
Thanks again for reading through my code...

Fotiman

5:50 pm on Oct 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You are passing a string value of "false" to your enableUs function rather than sending the boolean value false.

Remove the quotes around false and see if that works (and as rainborick mentioned, removed the 2nd argument from your call to getElementsByName).

sbrocks

6:57 pm on Oct 16, 2009 (gmt 0)

10+ Year Member



ah, thank you Fotiman, passing false as a value rather than a string worked.
Odd though, that the same routine, when passing "true" worked on the group of dropdowns. Maybe Javascript defaults to true when it's unclear about which value to use? Who knows.
I'm happy that is works now, thank you both for your help.
Sara

Fotiman

7:14 pm on Oct 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In JavaScript (as in many other languages), everything has a truthy or falsy value. The following values are always falsy:

false
0 (zero)
"" (empty string)
null
undefined
NaN (a special value meaning Not a Number)

All other values are truthy. Any non-empty string is truthy, including "0" (zero in quotes) and "false" (false in quotes).

So the reason "true" worked for you was because "true" is a non-empty string, so therefore will evaluate to true when coerced to a boolean value. :)

sbrocks

7:49 pm on Oct 16, 2009 (gmt 0)

10+ Year Member



Yet another "Ahhh!"
Have printed your explanation and added it to my wall. Hopefully I'll avoid the crazies again (at least of the truthy falsy sort).
Thank you!