Le Qs Le Qs - 11 months ago 41
Javascript Question

Getting the value of attribute data-* in an inline function

I have a data attribute

which i am using to store some data. I want to pass the data in
to a function as a second parameter to a function.

This is the code

<button onClick="editevent(this.id,this.id.getAttribute('data-niq'));" id="mid" data-niq="niq" class="mr edit btn btn-success pull-right"> Edit</button>

function editevent(clicked_id,attri_bute){
console.log('clicked id',clicked_id);

and the link https://jsfiddle.net/codebreaker87/zob8dm4z/8/

When i run the code i get
TypeError: this.id.getAttribute is not a function

How can i pass the
value in the inline function that i am calling?.

Answer Source

Few things here

  1. Never mix your mark up with javascript
  2. Try to bind events in javascript end.

check the following snippet.

window.onload = function() {

  var mid = document.getElementById("mid");
  mid.addEventListener('click', function() {

function editevent(thisObj) {

  var id = thisObj.getAttribute('id');
  var dataniq = thisObj.getAttribute('data-niq');
<button id="mid" data-niq="niq" class="mr edit btn btn-success pull-right">Edit</button>

Hope it helps