views:

62

answers:

1

Hey guys, I want to create a form which has a text entry box where a user can enter their name and then I want a button as well. But what I want this button to have a function called ReadName() where what will happen is when the user clicks on the button it will come up with a message saying "Hello user name will appear here I have tried my self and but I don't think I am not getting what I want. Any help will be appreciated.

<form>
    <label for="name">Name:</label>
    <input type="text" name="name" id="name"/>
    <input type="text" name="name" onfocus="ReadName()"/>
</form>
+4  A: 
function ReadName() {
    var name = document.getElementById('name').value;
    alert('Hello '+ name);
}
Squeegy
Thanks for this. Could you explain to me how you got this? i'm puzzled by the "document.getElementById('name')" bit. what does that mean?
`document.getElementById()` is a basic javascript function supported in all browsers. It is the simplest way to get an HTML node that has a specific `id`. And every `input` HTML node has a `value` property, giving the current value of that form field.
Squeegy