JavaScript gets code for event values such as OnClick, onchange, and so on _jquery

Source: Internet
Author: User
Today the side dishes handle Pull-down menu cascading problems, want to get the content of an event in the HTML tag, that is, the value, such as from <select id= "City" onchange= "Javascript:test ();" ></select> get Javascript:test ();.
Small dishes want to pass the information in the event to determine the next level of the menu, but this seemingly simple question, but let the side of the cake tangled.
A little bit of jquery's kid's shoes might try to get this:
Copy Code code as follows:

$ (document). Ready (function () {
var Onchangevalue = $ ("#city"). attr ("onchange");
alert (Onchangevalue);
});

In general, this can indeed be obtained, because the jquery universal attr method, can get any "properties" in the label, even an event, you can directly get content, here onchange is the event.
But the side dishes in the actual development environment, with this method how also cannot obtain, obtains is the undefined.
In the tangle, another method of using pure JavaScript to achieve the acquisition was found.
The specific code is as follows:
Copy Code code as follows:

$ (document). Ready (function () {
var onchangevalue = document.getElementById ("City"). GetAttributeNode ("onchange"). NodeValue;
alert (Onchangevalue);
});

To put it simply, this is the main use of the GetAttributeNode () method, it gets the attribute node, ignoring the difference between the properties and events, similar to the processing of XML, and then use NodeValue to get the node value of the attribute node.
If you use the GetAttribute () method, because OnChange is an event, you get a function object that cannot be treated as a string.
Hopefully this article will help the children's shoes in need.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.