aliento Posted July 3, 2008 Share Posted July 3, 2008 hello. i am using this code to call the javascript function and change the attribute . The code to call the js function is (with php) onclick="check('.$ii.','.$iii.')" and the check function is function check(n1,n2) { document.getElementById('n1').checked=true document.getElementById('n2').checked=true } but it doesnt work. Quote Link to comment Share on other sites More sharing options...
shaunmckinnon Posted July 3, 2008 Share Posted July 3, 2008 explain further. do you mean you're trying to call the onClick event in PHP? Quote Link to comment Share on other sites More sharing options...
aliento Posted July 3, 2008 Author Share Posted July 3, 2008 I am trying to parse to the js function 2 variables. Each will be the id of the checkboxes.A want to check those 2 checkboxes.I use the variables into document.getElementById('n1').checked=true which the variable is the n1 but it doesnt recognize it as variable! Quote Link to comment Share on other sites More sharing options...
shaunmckinnon Posted July 3, 2008 Share Posted July 3, 2008 I think you're trying to pass PHP variables to the javascript function. If that's the case, you may be approaching it wrong. ie: <html> <head> <script> function check(n1,n2) { document.getElementById('n1').checked=true; document.getElementById('n2').checked=true; } <script> </head> <body> <form> <input type="radio" id="n1" name="n1"> <input type="radio" id="n2" name="n2"> <input type="button" id="checker" name="checker" onClick="check('n1', n2');"> </form> </body> </html> [code] [/code] Quote Link to comment Share on other sites More sharing options...
shaunmckinnon Posted July 3, 2008 Share Posted July 3, 2008 that should work. if you're trying to pass PHP variables, you need to echo the variables in the javascript code. probably easier to <? $variable_1 = "unchecked"; $variable_2 = "checked"; echo '<input type="radio" id="" name="" value="'.$variable_1.'">'; ?> i would just block what i need to do in a small php block. still don't quite know what you're looking to do, but hopefully one of these code blocks can help decipher some hassle for you. Quote Link to comment Share on other sites More sharing options...
aliento Posted July 4, 2008 Author Share Posted July 4, 2008 Ok i will make it simple . lets say i got this js function function check(n1) { n1 = "c" + n1; document.getElementById('n1').checked=true; } or function check() { chu = "checke"; document.getElementById('something').chud=true; } // the chu is the variable. and with the d makes checked? The question is how the javascript recognize the variable. I know php and html, i can read javascript and 'write' but i dont know the basics. Quote Link to comment Share on other sites More sharing options...
aliento Posted July 4, 2008 Author Share Posted July 4, 2008 For the archive and better understudying i write the code below : php file : for($i=0;$i<count($fields);$i++) echo '<td>'.$fields[$i].'<input type="checkbox" name="field_check[]" id="c'.$i.'" /></td>'; //and for the onclick : for($iii=0;$iii<count($fields);$iii++) echo '<td onclick="check(\'c'.$iii.'\',\'c'.$ii.'\')">'; the javascript is : function check(n1,n2) { document.getElementById('n1').checked=true; document.getElementById('n2').checked=true; } I cant find any mistake. Can you? Quote Link to comment Share on other sites More sharing options...
adam84 Posted July 10, 2008 Share Posted July 10, 2008 Try this function check(n1,n2){ document.getElementById(n1).checked=true; document.getElementById(n2).checked=true; } Both n1 and n2 are variables, so when they are inside quotes, you are not using the variable, but rather just the string or whatever 'n1' and 'n2'. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.