Reputation: 15
For my current assignment I am suppose to build a website where people can log in, and post comment. I've never done web programming before so I've been struggling through it. right now I am confused as to how I can use the values from text fields on the html web page in the java script I'm using with the page.
this is the code from my Javascript, (it really doesn't do much of anything I'm just trying to figure out what I'm doing wrong)
<script type = "text/javascript">
function inform(name)
{
alert(name);
}
</script>
and this is what the html looks like where I call the function
<P ALIGN="right">
Email: <br><input type="text" name="fname"><br>
Pass Word: <br><input type="password" name="pass"><br>
<input type="button" value="Log In" onclick ="inform(document.getElementById('fname').value)"/>
</P>
When I run this and hit the button it says that the value of name in the inform function is always Null regaurdless of what I put in the name text field. What am I doing wrong?
Upvotes: 0
Views: 88
Reputation:
You've assigned a name
instead of an id
.
<!-----------------------------v------------v--->
Email: <br><input type="text" name="fname" id="fname"><br>
As an alternative, you could do this:
<input type="button" value="Log In" onclick ="inform(this.form.fname.value)"/>
And in fact, the this
is even optional:
<input type="button" value="Log In" onclick ="inform(form.fname.value)"/>
DEMO: http://jsfiddle.net/kPcmq/
Upvotes: 2
Reputation: 2113
You have to change this
Email: <br><input type="text" name="fname"><br>
to Email: <br><input type="text" name="fname" id ="fname" ><br>
Upvotes: 2