Jump to content

Parse js variable to attribute


aliento

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/113077-parse-js-variable-to-attribute/
Share on other sites

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]

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.

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.

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?

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'.

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.