Reputation: 5060
i know that jquery .attr()
is case insensitive to attribute name, but when i create an element from document created by code, it becomes case sensitive.
here is code sample that explain the problem:
var mydoc = document.implementation.createDocument('a:b','c',null);
var myElement = mydoc.createElement("myelement");
myElement.setAttribute("attr1","val1");
alert($(myElement).attr("attr1"));//val1
alert($(myElement).attr("Attr1"));//undefined! , why?
var myElement2 = document.createElement("myelement2");
myElement2.setAttribute("attr1","val1");
alert($(myElement2).attr("attr1"));//val1
alert($(myElement2).attr("Attr1"));//val1
i have tested it on chrome 22.0 and jquery 1.8.
UPDATE:
it seems that it is not jquery
issue.
console.log(myElement.attributes['attr1']); //val1 , OK
console.log(myElement.attributes['Attr1']); //undefined ???
console.log(myElement2.attributes['attr1']); //val1 , OK
console.log(myElement2.attributes['Attr1']); //val1 , OK
Upvotes: 4
Views: 687
Reputation: 13185
it's due to the type of documents, just specify the type of document (HTML for example):
var mydoc = document.implementation.createHTMLDocument()
http://jsfiddle.net/ouadie/wwNhw/
Upvotes: 1
Reputation: 195982
You need to create a documentType (https://developer.mozilla.org/en-US/docs/DOM/DOMImplementation.createDocumentType) of html and pass it to the createDocument
method as the 3rd parameter
var dt = document.implementation.createDocumentType('html', '', '');
var mydoc = document.implementation.createDocument('a:b','c',dt);
var myElement = mydoc.createElement("myelement");
myElement.setAttribute("attr1","val1");
console.log(myElement.attributes['attr1']); //val1 , OK
console.log(myElement.attributes['Attr1']); //val1 , OK !!!
Upvotes: 1