I know we could get the node name by using nodeName, but is it possible to set the nodeName using jquery as well? basically i wanted to chnage the casing of node name from uppercase to lowercase.
Thanks a lot
I know we could get the node name by using nodeName, but is it possible to set the nodeName using jquery as well? basically i wanted to chnage the casing of node name from uppercase to lowercase.
Thanks a lot
I believe that in order to do this properly, you need to replace that tag as described here: jQuery: how to change tag name?
Unfortunately, this would require you to replace the entire element and not just the tag name, so if you want to replace the tag name, you need to find a way to preserve both the tag's attributes as well as the code within.
Fortunately, I had an extra few minutes, so I created function that does just that:
function change_tag_name(selector,tagname){
var old_elm = $(selector);
//Create a jquery element based on selector
var original_elements = old_elm.get();
//Get the array of original dom objects
//Note: We get the original dom objects because jQuery objects do not provide access to the attributes array
for(var i=0; i< original_elements.length; i+=1){
var original_element = original_elements[i];
//Create reference to original element in dom
var new_elm = $(document.createElement(tagname));
//Create new element with desired tagname
new_elm.html($(original_element).html());
//Add original element's inner elements to new element
var original_attributes = original_element.attributes;
//Create an array of the original element's attributes;
var attributes = new Object();
//Create a new generic object that will hold the attributes for the new element
for(var j=0; j<original_attributes.length; j+=1){
var attribute = original_attributes[j];
var name = attribute.nodeName;
//Note: The attributes "nodeName","localName",and "name" appear to have the same value, so I just chose one. It's entirely possible that these could have different values in different contexts...
//var name = attribute.localName;
//var name = attribute.name;
var value = attribute.nodeValue;
//Note: The attributes "nodeValue","textContext",and "value" appear to have the same value, so I just chose one. It's entirely possible that these could have different values in different contexts...
//var value = attribute.textContext;
//var value = attribute.value;
attributes[name]=value;
}
//Populate attributes object
new_elm.attr(attributes);
//Assign attributes from old element to new element
$(original_element).replaceWith(new_elm);
//Replace old_element with new element
}
}
Also, I don't think that there's a way to guarantee that attributes are in the original order.
You can't change the nodeName, but you could create a new node with the different name, copy attributes across, then replace one with the other.
jQuery makes it hard to distinguish between attributes and properties, you may be able to use the IE proprietary outerHTML property (which is supported in a few other browsers) and string manipulation, but that won't work on the web in general.
If there are many such nodes, then a global replace of "