shreyaskudav Posted May 23, 2014 Share Posted May 23, 2014 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 More sharing options...
thescientist Posted May 23, 2014 Share Posted May 23, 2014 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. 1 Link to comment Share on other sites More sharing options...
shreyaskudav Posted May 23, 2014 Author Share Posted May 23, 2014 yaa got it working! (y) Link to comment Share on other sites More sharing options...
MrFish Posted May 24, 2014 Share Posted May 24, 2014 (edited) 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 May 24, 2014 by MrFish Link to comment Share on other sites More sharing options...
thescientist Posted May 24, 2014 Share Posted May 24, 2014 you forgot to add document.getElementById, so all your examples will throw errors in the console. Link to comment Share on other sites More sharing options...
MrFish Posted May 24, 2014 Share Posted May 24, 2014 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now