简体   繁体   English

如何使用javascript onclick()获取元素的属性值?

[英]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. 我想访问button的ID,当我编写PHP变量时,javascript代码创建错误“未定义”值。

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() { }); 您不需要$(function() { }); inside of your function. 在您的功能内部。 You have to pass this to your function so it knows which element was clicked. 您必须this传递给函数,以便它知道单击了哪个元素。

 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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