Solved

javascript code for document.evaluate (xpath) in IE

Posted on 2012-12-24
9
761 Views
Last Modified: 2012-12-25
Hello,
would you tell me how to write this code to support also IE?
if (!document.evaluate) {
  alert("Sorry, cannot do that. Your browser does not support the evaluate method!");
  return null;
}
var a_el = document.evaluate(".//tr/td/a[contains(@id, 'foo')]", table_node, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

Open in new window

Thanks, Aryeh.
0
Comment
Question by:tuchfeld
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 22

Expert Comment

by:Mrunal
ID: 38719607
Hi Aryeh,

I think IE does not support this property. My suggestion is go with jQuery.

Check this out:

http://stackoverflow.com/questions/4681968/document-evaluate-cross-browser
0
 
LVL 11

Accepted Solution

by:
mcnute earned 500 total points
ID: 38719794
To use jquery is your best option:

$('table tr td a').each(function(){
   if ( $(this).attr('id').match(/foo/g) ) {
   //  do something with it
   }
});

Open in new window

0
 

Author Comment

by:tuchfeld
ID: 38720077
Mcnute, can you tell me.
in your jquery code, where do I use the table_node variable?
(this is the ONLY table in the document I want to search in).
Thanks.
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 11

Assisted Solution

by:mcnute
mcnute earned 500 total points
ID: 38720189
if it is the only table in the page, then my code snippet will do. Otherwise give your specific table an id.

var table = $('#id_of_the_table').find('a');

table.each(function(){
   if ( $(this).attr('id').match(/foo/g) ) {
   //  do something with it
   }
});

Open in new window

0
 

Author Comment

by:tuchfeld
ID: 38720195
ok. thanks.
can I use table_node element instead of $('#id_of_the_table') ?
0
 
LVL 11

Expert Comment

by:mcnute
ID: 38720226
you can give whatever id you want, unless it begins with an alpha value from a-z. id's like 1-item are not allowed since it starts with a number.

So it can be $('#table_node'), yes.
0
 

Author Comment

by:tuchfeld
ID: 38720313
I mean, what if the table_node is DOM element?
retrieved somehow (it has NOT an id property).
e.g. table_node = some_element.parentNode.childNodes[3]; // etc.
0
 
LVL 11

Expert Comment

by:mcnute
ID: 38720325
If you know that it is the third table in the DOM, then you can do something like this:

$('table:eq(2)').find('a');

Open in new window


Considering that index starts at 0 so your first is 0, second table is 1, third is 2.

Am I understanding you right there?

You seam to know which table that is. You have a varialbe with the object of the table stored in it, right? You can just use it as is:

var tablea = table_node.find('a');

tablea.each(function(){
   if ( $(this).attr('id').match(/foo/g) ) {
   //  do something with it
   }
});

Open in new window

0
 

Author Closing Comment

by:tuchfeld
ID: 38720343
Thanks.
0

Featured Post

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

728 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