-2

undefined

HTML Code

<a id="int" onclick="level('int')" value="4">L1</a>

javascript Code:

function level(str) {
    var lev=document.getElementById(str).value;
    document.write(lev); // enter code here
}
Amit Joki
  • 58,320
  • 7
  • 77
  • 95
  • possible duplicate of [Why is document.write considered a "bad practice"?](http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice) – Paul S. Apr 10 '14 at 17:11
  • Try `getAttribute('value')` instead of `value` – Rahil Wazir Apr 10 '14 at 17:11

1 Answers1

1

Only form control elements have a value attribute and property. You can use data-* attributes instead:

<a id="int" onclick="level('int')" data-value="4">L1</a>

function level(str) {
    var lev=document.getElementById(str).getAttribute('data-value');
    document.write(lev);
}

A slightly better version of your code:

<a id="int" onclick="level(this)" data-value="4">L1</a>

function level(element) {
    var lev = element.getAttribute('data-value');
    console.log(lev);
}

Learn more about event handling.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Then you have a problem somewhere else in your code, or you didn't follow my suggestions properly, because it works fine: http://jsfiddle.net/dbaH8/. – Felix Kling Apr 10 '14 at 17:17