alert a variable value
A couple of things:
- You can't use
new
as a variable name, it's a reserved word. - On
input
elements, you can just use thevalue
property directly, you don't have to go throughgetAttribute
. The attribute is "reflected" as a property. - Same for
name
.
So:
var inputs, input, newValue, i;
inputs = document.getElementsByTagName('input');
for (i=0; i<inputs.length; i++) {
input = inputs[i];
if (input.name == "ans") {
newValue = input.value;
alert(newValue);
}
}
Note, while the above answers are correct, if you want, you can do something like:
alert("The variable named x1 has value: " + x1);
show alert box with use variable with message
<script>
$(document).ready(function() {
var total = 30 ;
alert("your total is :"+ total +"rs");
});
</script>