How do I add a custom attribute to a textnode in IE?

So, in my application I walk through the DOM nodes and add an attribute called elementNumber to each of them.

This works great in mozilla...to any node I just do:

node.elementNumber = "value";


But in ie, the syntax node.elementNumber fails if node has nodeType == 3. i.e. node is a textnode.

Thus, I am wondering how and if it is possible to add custom attributes like elementNumber to a textnode in interent explorer?

simsinghAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZvonkoSystems architectCommented:
In IE and Mozilla this is the better way:

node.setAttribute("elementNumber", "value");

You read it like this:

var theValue = node.getAttribute("elementNumber");


0
simsinghAuthor Commented:
Yes, but this fails if node happens to be a textnode in IE 6...

I was wondering if there is a way to get this working in IE 6?
0
ZvonkoSystems architectCommented:
Perhaps this works for you:

<html>
<head>
<title>Zvonko &#42;</title>
</head>
<body>
<div id="myDiv">My Text</div>
<script>
var theDiv = document.getElementById("myDiv");
var node = theDiv.childNodes[0];
alert(node.nodeName);
var elemNum = document.createAttribute("elementNumber");
elemNum.nodeValue = "value";
node.setAttributeNode(elemNum);
var theValue = xnode.getAttribute("elementNumberx");
alert(theValue);
</script>
</body>
</html>

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ZvonkoSystems architectCommented:
Sorry, it has a typo. But it does also not work with corrected typo: xnode
0
ZvonkoSystems architectCommented:
It does not work for #text
Only for DIV:
<html>
<head>
<title>Zvonko &#42;</title>
</head>
<body>
<div id="myDiv">My Text</div>
<script>
var theDiv = document.getElementById("myDiv");
//var node = theDiv.childNodes[0];
alert(theDiv.nodeName);
var elemNum = document.createAttribute("elementNumber");
elemNum.nodeValue = "value";
theDiv.setAttributeNode(elemNum);
var theValue = theDiv.getAttribute("elementNumber");
alert(theValue);
</script>
</body>
</html>

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
simsinghAuthor Commented:
Agreed that works for div but I need it to work for text...are you saying that it is impossible to set attributes to a textnode in IE?
0
ZvonkoSystems architectCommented:
So far I do not know of a way doing so.
But what about this:
<html>
<head>
<title>Zvonko &#42;</title>
</head>
<body>
<div id="myDiv"><text>My Text</text></div>
<script>
var theDiv = document.getElementById("myDiv");
var node = theDiv.childNodes[0];
node.setAttribute("elementNumber","value");
var theValue = node.getAttribute("elementNumber");
alert(theValue);
</script>
</body>
</html>

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.