2 Replies - 2662 Views - Last Post: 14 August 2012 - 09:06 PM

#1 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

getElementById undefined?

Posted 14 August 2012 - 04:09 PM

Why the alert shows bets="undefined?" if I change it to getElementById('btnOrder'), no problem?

<script type="text/javascript">
function test()
{
	var test = document.getElementById('bets').value;
	alert('bets= '+test);
}
</script>


<table>
<tr><td colspan=5># of bets: <span id="bets" value="none"></span></td></tr>
<tr><td colspan=5><input type="submit" name="btnOrder" id="btnOrder" value="place order" onclick="test()" /></td></tr>
</table>


Is This A Good Question/Topic? 0
  • +

Replies To: getElementById undefined?

#2 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: getElementById undefined?

Posted 14 August 2012 - 04:37 PM

getElementById can only be used on an <input> element?
Was This Post Helpful? 0
  • +
  • -

#3 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 800
  • View blog
  • Posts: 1,688
  • Joined: 30-January 09

Re: getElementById undefined?

Posted 14 August 2012 - 09:06 PM

You're getting undefined come back on the value attribute, not the getElementById call. This is because value is not a standard attribute for span elements. Because it is not a standard attribute, it becomes a custom attribute. If you want to access custom attributes, you need to use this:
var myValue = document.getElementById("bets").getAttribute("value");


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1