Javascript Passing Values Into Function


Passing Values Into Javascript Function

Here are two methods for which you can use DOM(Document Object Model) to use values within html forms.

Example 1:
When someone clicks the button, the value 25 become a parameter for the get_price() function.  It becomes 'value'.

<html><body>
<script>
function get_price(value){
var price1 = document.getElementById("myid").value;
//return true;
alert("Price is: $"+ price1 + ".00");
window.location.reload();
return true;
}
</script>

<div>Hats
    <form>
<input type="text" id="myid" name="myid" value="25"/>
<input type="button" id="thebutton" name="thebutton" onclick="get_price(this.form.myid.value)"/>
</form></div><div style="clear:both;"></div>
</body></html>

Example 2:
The code below just calls the get_price()  function without parameters. Then, using DOM we select the desired id for which to make the price1 variable.

<html><body>
<script>
function get_price(){
var price1 = document.getElementById("myid").value;
alert("Price is: $"+ price1 + ".00");
window.location.reload();
return true;
}
</script>

<div>Hats
    <form>
<input type="text" id="myid" name="myid" value="25"/>
<input type="button" id="thebutton" name="thebutton" onclick="get_price()"/>
</form></div><div style="clear:both;"></div>
</body></html>