![]() Var btn = document.querySelector("button") On clicking on the button, the background color would change to yellow as specified in setAttribute(). Since there is no attribute style “ background”, the button shows the default background color. Let us now see what happens if the attribute is not present, considering the above example. Since the attribute is present, the value will change to yellow.Then we are adding to attribute value style and background color as yellow.In the function, we are getting the item using ‘document.getElementById(“button”)’.When the button gets clicked, javascript function fun_color is called.Id, onclick, and style are the attributes.On clicking on the button, the background color would change to yellow as specified in setAttribute() Var elem = document.getElementById("button") Įlem.setAttribute("style", "background:yellow") ![]() Var element = document.getElementById('sample') Į = "yellow" Here, after clicking on Try it, link is the href attribute set to value and now looks like a link. Example #2 Ĭlick the button to set the href attribute with a value of "of the element idĭocument.getElementById("sampleAnchor").setAttribute("href", "") HTML elements define all javascript properties to standard HTML attributes hence while trying to set the attributes to non-standard attributes, the user needs to use javascript setAttribute() function. This example shows how an input field can be modified to an input button. Let us see few examples on how to use setAttribute() Example #1 ĭocument.getElementsByTagName("input").setAttribute("type", "button") Īfter clicking on the Click Here button the output will be as shown below. *setAttribute() javascript method does not return any value. Since, the specified value gets converted to a string, specifying ‘null’ value sets’s attribute value to string ‘null’ whose return value will be ‘undefined’. Parameters name specifies the name of the value whose value is to be set and value specifies the value which is to be assigned to the attribute. ![]() Web development, programming languages, Software testing & others ![]() Start Your Free Software Development Course
0 Comments
Leave a Reply. |