Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-19-2012, 03:44 AM   PM User | #1
johnsmith153
New Coder

 
Join Date: Mar 2012
Posts: 81
Thanks: 7
Thanked 0 Times in 0 Posts
johnsmith153 is infamous around these parts
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]');
	
for (i = 0; i < e.length; i++) {
 
}
johnsmith153 is offline   Reply With Quote
Old 12-19-2012, 05:25 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,209
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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
// first, remove all the <li>s
while ( ul.hasChildNodes )
{
    ul.removeChild( ul.lastChild );
} 
// now build the replacement element:
var inp = document.createElement("input");
inp.className = ul.className; //clone class name
inp.value = ul.getAttribute("data-value");
// if you wanted any other attributes copied, you would need to do so one by one
//
// now replace the <ul> with the <input>
ul.parentNode.replaceChild( inp, ul );
Should work. There's no easy way to copy all the data-xxx properties except your chosen ones. You could iterate through all the properties, looking for certain names, but that's tedious. Surely, in real life, you would know exactly which properties you are going to keep?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

Last edited by Old Pedant; 12-19-2012 at 05:31 AM..
Old Pedant is offline   Reply With Quote
Old 12-19-2012, 05:26 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,209
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 12-19-2012, 05:33 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,209
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
If you wanted the code to be universal, you could do:
Code:
var uls = document.getElementsByTagName("ul");
for ( var u = 0; u < uls.length; ++u )
{
    var ul = uls[u];
    if ( ul.getAttribute("data-abc") != null )
    {
        ... same code as above performed on the ul object ... 
    }
}
That should work even in MSIE 7.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:06 PM.


Advertisement
Log in to turn off these ads.