-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
  • 53,955
  • 7
  • 67
  • 89
  • 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
  • 705,106
  • 160
  • 1,004
  • 1,072
  • 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