Solved

javascript code for document.evaluate (xpath) in IE

Posted on 2012-12-24
9
645 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how can i extend ? 2 43
show form submit button based on jquery sum 5 28
Simple Total function not working 8 32
how can i get the string decoded ? 2 14
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

777 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