0
<span id="roleDropName" class="dropdown_btn_val" dropopt="1">Thamaraiselvam</span>

this is my code i need to get dropopt using above id or class and i did following code it shows nothing to me

console.log(document.getElementById("roleDropName").dropopt);
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143

2 Answers2

6

None of the DOM elements has a property dropopt, so you get back the value undefined.

If you want to access custom attributes, you have to use getAttribute:

document.getElementById("roleDropName").getAttribute('dropopt');

However, custom attributes are better defined as data-* attributes. Then you can also use dataset in browsers that support it.


Related questions:

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
2

Here is the jquery version. First you need to get the id name, and then you can use .attr() function which is to get the attribute value or even added the new one.

console.log($('#roleDropName').attr('dropopt'));
Norlihazmey Ghazali
  • 9,000
  • 1
  • 23
  • 40