![]() |
Non-jQuery way to change HTML / the DOM
I'm looking at a non-jQuery way to change HTML / the DOM.
So if I have this: <ul class="class-name" data-value="the-value" data-abc="1"> <li>etc.</li> <li>etc.</li> <li>etc.</li> </ul> … and I want to change it to: <input type="text" class="class-name" value="the-value" /> So, that would: - remove all <li> inside - convert to <input type="text"> (or another specified input type) - convert the data-value attribute to a value attribute - maintain any other attributes if applicable (e.g. an id) - remove the data-abc attribute (not all data- attributes, just a chosen one) … this isn't any specific need, I just want to be able to make changes without jQuery. I will usually always grab the required element from a data- attribute, and I have this to start me off. Can someone help me compete this so I can create all other ones myself? Code:
var e = document.querySelectorAll('[data-abc]'); |
I assume you know that querySelectorAll is not available with MSIE 7 and below? Do you care?
Anyway, it seems simple enough. Code:
var ul = document.querySelectorAll('[data-abc]')[0]; // assumes there is only one match |
By the by, isn't an <input> without either ID or NAME kind of useless?
And if you didn't notice, I did not set the type of the <input>. text is the default type, so why bother? |
If you wanted the code to be universal, you could do:
Code:
var uls = document.getElementsByTagName("ul"); |
| All times are GMT +1. The time now is 08:45 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.