Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Selectors

I am trying to take this from view source, i.e.:

<a  href="javascript:validateCB();"><img src="wwv_flow_file_mgr.get_file?p_security_group_id=1343380920146312332&p_flow_id=222&p_fname=submit_btn.gif" alt="Submit Details" border="0"  />

into jQuery selector format

var $templateButtons = $('img[src^="wwv_flow_file_mgr"]').parent('a');

But this doesn't seem to be correct.

How can I translate the above view source code into jQuery?

like image 741
tonyf Avatar asked Jun 17 '09 15:06

tonyf


People also ask

What are the jQuery selectors?

jQuery selectors allow you to select and manipulate HTML element(s). jQuery selectors are used to "find" (or select) HTML elements based on their name, id, classes, types, attributes, values of attributes and much more. It's based on the existing CSS Selectors, and in addition, it has some own custom selectors.

What are the four types of selectors?

Simple selectors (select elements based on name, id, class) Combinator selectors (select elements based on a specific relationship between them) Pseudo-class selectors (select elements based on a certain state) Pseudo-elements selectors (select and style a part of an element)

How many standard jQuery selectors are there?

Projects In JavaScript & JQuery This tutorial will explain jQuery Selectors with simple examples covering all the three standard selectors.


2 Answers

The problem is with the attribute selector: $('img[src^="wwv_flow_file_mgr"]')

You're experiencing a known bug in jQuery v1.3.2 - jQuery is trying to interpret the image path using its absolute URL, which means the URL it's comparing actually starts with "http://..."

You can temporarily get around this using *= (which looks for an attribute value that contains the text "wwv_flow_file_mgr" instead of starting with it):

var $templateButtons = $('img[src*="wwv_flow_file_mgr"]').parent('a');
like image 127
John Rasch Avatar answered Oct 16 '22 07:10

John Rasch


This is very odd, it doesn't work with the selector you have, although, I think it should.

However, while not very clean, it does work using a filter as shown below.

var $templateButtons = $("img")
                       .filter(function(){
                             return $(this).attr('src')
                                    .indexOf('wwv_flow_file_mgr')==0;
                       }).parent('a');
like image 31
Jose Basilio Avatar answered Oct 16 '22 07:10

Jose Basilio