简体   繁体   中英

How to get the attribute value of an element using javascript onclick()?

I want to access the id of button , when i write the PHP variable the javascript code create error "Undefined" value.

function update_profile(){ $(function() {
var element = $(this);
var update_id = element.attr("id");
  var infoo = 'updated_id=' + update_id;

    document.getElementsByClassName('test')[0].innerHTML=infoo;


});
}

button code:

<button data-toggle="modal" data-target=".bs-example-modal-lg" onclick="update_profile()"  class="btn-xs btn-warning " id='<?php echo $user_rec[1]; ?>' >Update</button>

You don't need $(function() { }); inside of your function. You have to pass this to your function so it knows which element was clicked.

 function update_profile(element){ var update_id = element.id; var infoo = 'updated_id=' + update_id; document.getElementsByClassName('test')[0].innerHTML = infoo; } 
 <div class="test">test class element</div> <button data-toggle="modal" data-target=".bs-example-modal-lg" onclick="update_profile(this)" class="btn-xs btn-warning " id='idName' >Update</button> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM