0

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

Tintin
  • 2,853
  • 6
  • 42
  • 74
  • You can parse XML with `$.parseXML`, and then wrap it in `$(xml)` , and treat it like html, using `find()`, `attr()` etc. but as what you're referring to as a nodename likely then would be a tagName, you probably can't change it. All you really have to do is try it out and see if it works? There's always `string.replace()` ? – adeneo Nov 28 '12 at 22:43
  • yeah... but string manipulation would be an overkill... wanted to avoiid it... can i use renameNode() method from broswer for custom xml? – Tintin Nov 28 '12 at 22:45
  • Have no idea, why don't you try it out and tell us if it works. I tried [**FIDDLE**](http://jsfiddle.net/BwBsn/) and could'nt get that work? – adeneo Nov 28 '12 at 22:47

2 Answers2

3

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.

Community
  • 1
  • 1
John Henry
  • 2,419
  • 2
  • 20
  • 22
0

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 "

RobG
  • 142,382
  • 31
  • 172
  • 209