Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

javascript code for document.evaluate (xpath) in IE

Posted on 2012-12-24
9
Medium Priority
?
873 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 11

Assisted Solution

by:mcnute
mcnute earned 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
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.
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…

598 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