user1823706
user1823706

Reputation: 53

create a variable from a class's other class

Trying to create a variable from a .class's second class.

var post_id = $('.divclass').hasClass('');

$(document).ready(function(){
    $(post_id).click(function() {
        $(this).fadeIn(1000);
    });
});

I know this is wrong, but maybe someone here can help make sense of that I'm trying to do. Thanks in advance.

Upvotes: 2

Views: 75

Answers (3)

Sampath
Sampath

Reputation: 65920

If you're having 2 classes like below :

<div id="trash" class="a b">
    <p>sample</p>
</div>

Then you can use jQuery selector is as below :

$(document).ready(function(){
    $('.a.b').click(function() {
        $(this).fadeIn(1000);
    });
});

I hope this will help to you.

Upvotes: 1

Konstantin Dinev
Konstantin Dinev

Reputation: 34905

Your post_id is a boolean. You are trying to attach an event handler to a boolean, when you should instead be attaching it to a DOM element. Don't use has class, but instead retrieve the class attribute:

var post_id = $('.divclass').attr('class');
post_id = post_id.replace('divclass', '');

Upvotes: 1

Jamie Hutber
Jamie Hutber

Reputation: 28096

So what you'll need to do is select the item with more than one class which you are doing:

var post_id = $('.divclass').attr('class');
//Now spilt the string by all of the spaces
post_id.split(" ");
//now refer to the string as an array
//lets get the second one.
post_id[1]

So for your case

//Added selector in this case a class with '.' this can be changed to be appropriate i.e '#' for an ID
$('.'+post_id[1]).click(function() {
    $(this).fadeIn(1000);
});

Upvotes: 3

Related Questions