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
Share on other sites

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!

Link to comment
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]

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.