calling javascript function from onchange

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



calling javascript function from <select> onchange



I am trying to call a javascript function from the onchange attribute of the select tag ! My issue is that I am passing the name attribute of the select to the function which always go null.


select


<body>


<form action="" method="post">
<select name="slct" id="name" onchange="rohan('measurement_conversion', '<?php echo isset($_POST["slct"])?$_POST["slct"]:"null" ?>')">
<option value="yes" selected="selected"> yes </option>
<option value="nopes"> nopes </option>
<option value="may be"> May be </option>
<option value="dont know"> dont know </option>
</select>
</form>

<div id="abc">
</div>


</body>



And here my javascript function


<script>

function rohan(var1, var2)


document.getElementById("abc").innerHTML=var1 + " " + var2;


</script>



It always prints null..
Any help will be appreciated !




3 Answers
3



HTML:


<body>
<form action="" method="post">
<select name="slct" id="name" onchange="rohan(this.value)">
<option>Select</option>
<option value="yes" selected="selected"> yes </option>
<option value="nopes"> nopes </option>
<option value="may be"> May be </option>
<option value="dont know"> dont know </option>
</select>
</form>
</body>



JS:


<script>
function rohan(value)

//you can get the value from arguments itself
alert(value);

</script>



PHP is run on the server before JavaScript is ever run in the browser. It doesn't get re-evaluated when the user changes the selected item.





So what will be the solution ?
– Saaram
Sep 22 '12 at 7:56





Code that reacts to user changes needs to either (a) be written in JavaScript, not PHP... or (b) use AJAX.
– Amber
Sep 22 '12 at 7:56



No put in tag HTML attribute javascript, onchange is better in the script:


onchange


<script>
var b=document.getElementById('name');
b.onchange=function ()
var a=document.getElementById('name').value;
console.log(a);

</script>






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard