Jump to content

Passing variable to getElementbyId()


shreyaskudav

Recommended Posts

Is it possible to pass javascript variable in getElementbyId()??

 

This is my radio code:

echo '<td><input id="dis1" type="radio" name="select" value="1" /></td>';echo '<td>'.$poke[0]."<br>";echo 'Level:'.$level[0]."<br>";echo 'Health:'.$health[0]."<br></td>";

The below is my javascript code...in which first the id value is stored in PHP cvariable and then stored in javascript and using this javascript variable, I will disable the radio button. !

<script>var dis_value = <?php echo $dis_value; ?>;                          //$dis_value = dis1document.getElementById("dis_value").disabled=true;</script>

But dunno why it seems not to be working?? are there any constraints which I need to follow if I am using a variable in getElementbyID ???

Link to comment
Share on other sites

you're not using the variable. you're literally passing the value "dis_value" to it. You need to pass the variable itself, not in quotes.

  • Like 1
Link to comment
Share on other sites

oh shreyaskudav.

 

You remind me of my younger self. I asked the very same question when I was 12 about booleans on the xgenstudios forums. You can always pass variables. The way programming languages work is that it always interprets the most nested elements first. So when you say-

var myVar = "test";getElementById(myVar);

It will interpret to

getElementById("test");

first.

 

Same with functions.

function foo() {  return "test";}getElementById(foo());

Interpets to-

getElementById("test");

Happy programming!

Edited by MrFish
Link to comment
Share on other sites

you forgot to add document.getElementById, so all your examples will throw errors in the console.

Link to comment
Share on other sites

you forgot to add document.getElementById, so all your examples will throw errors in the console.

 

Gotta love gin.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...