user1775888
user1775888

Reputation: 3313

How to select div contains parent div attr?

I try to select img in .sub rel='0' and trigger click, looks like $('.sub[rel=0] img').trigger('click') , but it does't work.
What is wrong with this selector code $('.sub[rel=0] img')

Thanks!

<div class="sub" rel="0">
    <img>
</div>
<div class="sub" rel="1">
    <img>
</div>

Update

function geturlvars(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++){
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
return vars;
};

realid = geturlvars()["id"]
function checkstate(){
    if(realid){
        $('.sub[rel=realid] img').trigger('click');
    }
    $('.sub[rel=realid] img').click(function(){
        console.log('hi');
    });
};
checkstate();

Upvotes: 1

Views: 121

Answers (3)

jQuerybeast
jQuerybeast

Reputation: 14500

This is what you are looking for:

$(".sub[rel=0]").click(function(){ alert("Click Triggered!");  })

     function geturlvars(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++){
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    } return vars; };

realid = geturlvars()["id"] function checkstate(){

    realid = "0";  // delete this
    if(realid){
        $('.sub[rel='+realid+'] img').trigger("click");
    }
         }; 
checkstate(); ​

Make sure the click function is a global function and categorize the variables from the text

$('.sub[rel=realid] img')        // Wrong
$('.sub[rel='+realid+'] img')    // Correct

Fiddle: http://jsfiddle.net/javascript/T7uss/

Upvotes: 2

Travis J
Travis J

Reputation: 82297

Nothing seems to be wrong with that situation...

html:

<div class="sub" rel="0">
 <img>
</div>
<div class="sub" rel="1">
 <img>
</div>​

js:

console.log($('.sub[rel=0] img')[0]);​

console:

<img>

jsfiddle: http://jsfiddle.net/PHrRY/

moreover

Here is an example where there is a triggered click event. For you to trigger a click event on that image, it must have an attached event. More than likely if there is no event, nothing happens, and that is why your trigger is not actually firing an event off. This code will fire the event using trigger.

jsfiddle: http://jsfiddle.net/PHrRY/1/

edit

Here is an example where the 0 for rel is stored in a variable:

summary: var relNumber = "0 0"; console.log($('.sub[rel="'+relNumber+'"] img')[0]);

http://jsfiddle.net/PHrRY/3/

The extra " quotation marks are used in case there is a space inside of the relNumber.

In response to update

this code should be

if(realid){
    $('.sub[rel="'+realid+'"] img').trigger('click');
}
$('.sub[rel="'+realid+'"] img').click(function(){
    console.log('hi');
});

Upvotes: 2

Fidi
Fidi

Reputation: 5824

I don't really know if they are necessary, but I am always using " when I want to select a value from an attribute:

$('.sub[rel="0"] img');
           -^-^-

Maybe this does the trick.

Upvotes: 0

Related Questions