<select data-myattr="123" id="cboTest"
onchange="SayHello(this.data-myattr)">
This doesn't work. If I take the data-
off of it it, it works, but from what I've read, it is html5 safe to do it that way. However, doing it that way, I get: "Microsoft JScript runtime error: 'myattr' is undefined".
This is my super-complex function:
function SayHello(msg) {
alert(msg);
}
Thanks in advance.
If you want to define your own custom attributes in HTML, you can implement them through data-* format. * can be replaced by any of your names to specify specific data to an element and target it in CSS, JavaScript, or jQuery.
the data attribute value passed to custom element can be used while rendering the element. Let's look for the code required on the JavaScript side to extract the value passed with the attributes. In the above code, we are accessing the data sent in the attributes using the variable value “this. attributes”.
Custom attributes. A custom attribute is a property that you can define to describe assets. Custom attributes extend the meaning of an asset beyond what you can define with the standard attributes. You can create a custom attribute and assign to it a value that is an integer, a range of integers, or a string.
You can add custom HTML attributes to pages and page elements, which are rendered on the HTML tag of the page element. For example, this is useful when working with third-party frameworks that render page elements differently based on certain attributes.
Try like this:
<select data-myattr="123" id="cboTest"
onchange="SayHello(this.getAttribute('data-myattr'))">
The expression:
onchange="SayHello(this.data-myattr)">
Is being interpretted as this.data
minus myattr
. You'd have to use:
onchange="SayHello(this.getAttribute('data-myattr'))"
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With