What is unobtrusive javascript? How to add behavior to an element using javascript?

Answer

Unobtrusive Javascript refers to the argument that the purpose of markup is to describe a document’s structure, not its programmatic behavior and that combining the two negatively impacts a site’s maintainability. Inline event handlers are harder to use and maintain, when one needs to set several events on a single element or when one is using event delegation.

1
<input type="text" name="date" />

Say an input field with the name “date” had to be validated at runtime:

1
2
3
4
5
6
document.getElementsByName("date")[0].
                   addEventListener("change", validateDate, false);
 
function validateDate(){
// Do something when the content of the 'input' element with the name 'date' is changed.
}

Although there are some browser inconsistencies with the above code, so programmers usually go with a javascript library such as JQuery or YUI to attach behavior to an element like above.

All javascript Questions

Ask your interview questions on javascript

Write Your comment or Questions if you want the answers on javascript from javascript Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---