开发者

Can I dynamically take a Html element onclick attribute and use it in javascript?

开发者 https://www.devze.com 2023-04-03 17:32 出处:网络
<inputtype=\"button\" onclick=\"???\"开发者_高级运维id=\"1\" value=\"Button1\"/> Without knowing what the name of the function in the onclick attribute, can I take the function from the onclic
    <input  type="button" onclick="???"    开发者_高级运维id="1" value="Button1"   />

Without knowing what the name of the function in the onclick attribute, can I take the function from the onclick attribute and call it in javascript? If so, how can I do that?


If you just want to trigger click event which will call the onclick function, you can do something like this, using jquery

$('#1').click()


If you're using jQuery:

eval($('#1').attr('onclick'));


This should work, NOTE: IDs are not allowed to start with a number:

<input  type="button" onclick="???"    id="Button1 value="Button1"   />

document.getElementById("Button1").onclick();

Example

The way to do it in jQuery would be $('#Button1').trigger('click') using trigger

0

精彩评论

暂无评论...
验证码 换一张
取 消