PDA

View Full Version : Javascript... on radiobox checked disabled="false"



Duck
June 4th, 2007, 04:53 AM
Hi im trying to create a simple script in javascript that disables and enables a text field when u press a certain checkbox.

Problem is that the checkboxes are build with php and cant be given an ID.
So ive tried to do it with grabbing the element and that works.
Now i want to make it easier for me to adjust so i dont have to make 6000 different script for different checkboxes.


it should work with document.formnaam.radionaam[nummer].checked
but im getting undefined when i check it. seems it doesnt pick the names up because of the dots... Im not sure how to write it to make it work...

Thanks.


function textfieldCheck(naam, radionaam, nummer) {
//alert(document.formnaam.radionaam[nummer].value);
var radiopath = naam + . + radionaam + [ + nummer + ] + .checked
alert(radiopath);
if (radiopath == 1) {
alert('tog wel')
}
//alert(radiopath);
//var textfield = radiopath + "_text"
/*if (document.naam.radionaam[nummer].checked == 1) {
document.naam.textveld.disabled = false;
} else {
document.naam.textveld.disabled = true;
}*/
}

ratherblue
June 4th, 2007, 05:45 AM
"Problem is that the checkboxes are build with php and cant be given an ID."

How can you not give them an id? Can you post your PHP code?

Duck
June 4th, 2007, 06:08 AM
Hehe reason i cant give them an id is because this is my first script in php...
Here's the code.
Thanks for ur help :)


<?php
$sHiddenFields = "";
$sQuestion2 = (isset($_POST['s_sQuestion2'])) ? $_POST['s_sQuestion2'] : "";
$sSelectedQ1 = "";
$sFields = "";
$aNamen = array("via de link op de internetsite “aluminium.pagina.nl”", "via een andere link", "via een zoekmachine", "ik ben door iemand op de site geattendeerd", "anders, namelijk");

foreach($aNamen as $iK => $sValue) {
$sSelected = (isset($_POST['s_sQuestion2']) && $sValue == $_POST['s_sQuestion2']) ? "CHECKED" : "";
$sFields .= "<tr><td><span class=\"formtext\">" .$sValue . "</span></td><td><input type=\"radio\" name=\"s_sQuestion2\" value=\"" . $sValue . "\"" . $sSelected . " onClick=\"textfieldCheck('document.form2', 'sQuestion2', '4')\" /></td></tr>\n";
}

if(isset($_POST)) {
foreach($_POST as $sFieldName => $sValue) {
if(eregi("^s_", $sFieldName)) {
$sHiddenFields .= '<input type="hidden" name="' . $sFieldName . '" value="' . $sValue . '">' ."\n";
}
}
}

?>
<table border="0" cellspacing="0" cellpadding="0" style="color: #FFF;">
<tr>
<td colspan="2">
<form method="post" action="index.php" name="form2">
<input type="hidden" name="form" value="2">
<?php echo $sHiddenFields; ?>
</td></tr>
<tr><td colspan="2" class="form-headline">Via welke weg bent u op onze website terechtgekomen?</td><td></td></tr>
<?php echo $sFields; ?>
<tr><td><textarea name="s_sQuestion2_text" row="5" cols="30" disabled="true" class="form_textarea">Anders namelijk...</textarea></td></tr>
<tr><td><br /></td></tr>
<tr><td><input type="image" name="" src="../img/b_verder_green.gif" style="float:left;" /></td>
</table>
</form>

ratherblue
June 4th, 2007, 11:10 AM
your code:

var radiopath = naam + . + radionaam + [ + nummer + ] + .checked

try changing it to...

var radiopath = naam + '.' + radionaam + '[' + nummer + ']' + '.checked';

In Javascript, when you want to concatenate strings together, you have to use quotes around the strings that are not javascript variables.

I hope that helps.

Duck
June 5th, 2007, 03:01 AM
your code:

var radiopath = naam + . + radionaam + [ + nummer + ] + .checkedtry changing it to...

var radiopath = naam + '.' + radionaam + '[' + nummer + ']' + '.checked';In Javascript, when you want to concatenate strings together, you have to use quotes around the strings that are not javascript variables.

I hope that helps.
thanks ill give it a go...