Home > DeveloperSection > Forums > Why am I not able to append an image only if certain classes exist?
Jayden Bell
Jayden Bell

Total Post:105

Points:739
Posted on    April-09-2013 1:56 AM

 JQuery JQuery 
Ratings:


 1 Reply(s)
 968  View(s)
Rate this:
Hi Everyone!

I am trying to use jQuery to append an image tag to a div only if one of three classes exists, but it doesn't seem to be working. It either appends to all 

of the elements or none at all.

Here is the HTML code:

<figure class="donut minus">
    <div class="figure-value"></div>
</figure>
Here is the jQuery:

if ($("figure.donut").hasClass("minus") || $("figure.donut").hasClass("plus") || $("figure.donut").hasClass("plus-minus")) {
        $(".figure-value").append('<img src="../images/schoolProfile/donut-plus-minus.png" />');
    }

Thanks in advance


AVADHESH PATEL

Total Post:604

Points:4228
Posted on    April-09-2013 7:37 AM

Hi Jayden!

you can try this way

$("figure.donut")
    .filter(".plus, .minus, .plus-minus")
    .find(".figure-value")
    .append('<img src="../images/schoolProfile/donut-plus-minus.png" />');

This will find all of the figures with a class of donut and plus, minus or plus-minus and then find the element with a class of figure-value inside them and append the image.

In answer to the comment below, change the append to create a jQuery object and apply the css to that:

.append($('<img src="../images/schoolProfile/donut-plus-minus.png" />')
            .css({"top": -100+"px", "left": 0+"px"}));

I hope it solve your problem


Don't want to miss updates? Please click the below button!

Follow MindStick