Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2006-04-01
9
Medium Priority
?
256 Views
Last Modified: 2008-01-09
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?

0
Comment
Question by:simsingh
  • 5
  • 2
7 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 16351787
In IE and Mozilla this is the better way:

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

You read it like this:

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


0
 

Author Comment

by:simsingh
ID: 16351790
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 16351864
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 63

Expert Comment

by:Zvonko
ID: 16351876
Sorry, it has a typo. But it does also not work with corrected typo: xnode
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 2000 total points
ID: 16351891
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
 

Author Comment

by:simsingh
ID: 16351984
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 16352226
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question