TriAdmin a écrit :
The following code is throwing an error when the page loads: Error:
document.getElementById("custom_187") is
nulldocument.getElementById("custom_187").onchange = function(e) {
alert("kjhsdkhsdkh");
}
Sorry, I am not a javascript expert - much appreciate any help!
<script type="text/javascript">
function init() {
var chp = document.getElementById("custom_187");
if(!chp) alert('this field doesn\'t exist');
else
{
chp.onchange = function() {
alert('this field\'s value is : '+this.value);
}
}
}
window.onload = init;
</script>
Take care your inputs must have an id
<form blah ... >
<input name="custom_187" id="custom_187" >
<input name="custom_187_b" id="custom_187_b" >
....
</form>
variante :
<html>
<script type="text/javascript">
function updateField(field) {
field.form[field.name+'_tax'].value = field.value * 19.6 / 100;
field.form[field.name+'_total'].value = field.value * 1.196;
}
function init() {
var chp = document.getElementById("custom_187");
if(!chp) alert('this field doesn\'t exist');
else
{
chp.onchange = function() {
updateField(this);
}
}
}
window.onload = init;
</script>
<form>
amount: <input name="custom_187" id="custom_187" >
taxes: <input name="custom_187_tax" id="custom_187_tax" >
total amount: <input name="custom_187_total" id="custom_187_total" >
</form>
</html>
--
sm