Forum Moderators: open

Message Too Old, No Replies

Get element by id

Generating an alert box

         

Adam5000

5:16 pm on Sep 15, 2010 (gmt 0)

10+ Year Member



I'm trying to define a variable as the text a user enters into a form by the getElementById method. And then display that information in an alert box. But I'm not having any luck. Below is the code I've got. I have it working with

var a = document.form_one.abc.value;

but I'm thinking that getElementById is a more updated way.

Help!


<html>

<head>
<title>get element by id test</title>
</head>


<body>

<script type="text/javascript">

function g_test()
{
var a = document.getElementById('M_one');
alert(a);
}

</script>

<form name="form_one">

<input type="text" name="abc" id="M_one">
<input type="button" value="Click to test" onClick= "g_test()">

</form>


</body>
</html>

Fotiman

5:20 pm on Sep 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Try this:
function g_test()
{
var a = document.getElementById('M_one');
alert(a.value);
}

You're getting the element, but you're not alerting the value, you're alerting the element itself. :)

Adam5000

8:54 pm on Sep 15, 2010 (gmt 0)

10+ Year Member



Thanks again Fotiman. That fixed the trouble.

Applause and accolades to you!