PDA

View Full Version : Resolved $ sign as shortcut for getelementbyid



keyboard
02-20-2012, 10:13 AM
Can anyone tell me why this won't work?



<html>
<head>
<script type="text/javascript">
function $(element) {
return document.getElementById(element);
}
function bob() {
var x = $('text1');
alert(x);
}
</script>
</head>
<body>
<a href="#" onclick="bob()">Hello</a>
<input type="text" value="54321" id="text1">
</body>
</html>

jscheuer1
02-20-2012, 11:14 AM
Works fine here. In Firefox I get:


[object HTMLInputElement]

In IE I get:


[object]

Both of which are accurate. If you were expecting the element's value, then:


<html>
<head>
<script type="text/javascript">
function $(element) {
return document.getElementById(element);
}
function bob() {
var x = $('text1').value;
alert(x);
}
</script>
</head>
<body>
<a href="#" onclick="bob()">Hello</a>
<input type="text" value="54321" id="text1">
</body>
</html>

keyboard
02-21-2012, 02:22 AM
Ahh, that explains it. Thanks jscheuer1!