If I create a new DOM element with Javascript document.createElement, is there a way to use jquery function attr() to change the element's attribute?
3 Answers
you can use javascript like :
var link = document.createElement('a');
link.setAttribute('href', 'mypage.htm');
or jquery
$(link).attr('href', 'mypage.htm');
2 Comments
yilmazhuseyin
(for the first method) if you will add an event (like onclick). maybe you should just use link.onclick instead of link.setAttribute source = justinfrench.com/notebook/javascript-setattribute-vs-ie
Christian C. Salvadó
@yilmazhuseyin: Yeah, that's why I always recommend to use DOM properties over
setAttribute.