2

Is there a way to add some html based on a certain class name and inject it into a specific place. The code belows shows what i am trying to do, but this is obvioulsy hard coded, i want to do it dynamically using jQuery.

<ul class="status clrfix">
<li class="green"><a href="">Completed</a><span class="arrow"></span></li>
<li class="green"><a href="">Completed</a><span class="arrow"></span></li>
<li class="blue"><a href="">In Progress</a><span class="current-stage"><img src="images/current-stage.png" /></span><span class="arrow"></span></li>
<li>Not Started <span class="arrow"></span></li>
<li class="last">Not Started <span class="arrow"></span></li>

So, essentially what i want to do is; if the class="blue" then add this inside the li:

<span class="current-stage"><img src="images/current-stage.png" /></span>

This is what i attempted, but it's not working:

$(document).ready(function() {  

if($('ul.status li').hasClass('.blue')){
    $("span.current-stage").empty().html('<img src="../images/current-stage.png" />');
}

});

4 Answers 4

2

Try changing your if statement to the following:

if($('ul.status li').hasClass('blue')){

The hasClass() method accepts a class name as a parameter, not a selector. So you can leave out the . on the front of the class.

Also, if you wish to add HTML to without removing what is already there, use the append() function.

$("span.current-stage").append('<span class="current-stage"><img src="../images/current-stage.png" /></span>');
Sign up to request clarification or add additional context in comments.

3 Comments

Wow! that was fast and it is injecting the image fine. Howver, what i would really like to do is inject not only the image, but the span also, something like this:
if($('ul.status li').hasClass('blue')){ $().empty().html('<span class="current-stage"><img src="images/current-stage.png" /></span>'); }
No worries, I've updated the answer to show you how to "add". Using the html() function will basically replace what's already there and clear your In Progress text. So I'd suggest using append() instead.
0

Maybe something like this?

$('ul.status li.blue span.current-stage').empty().html('<img src="../images/current-stage.png" />');      

Comments

0

I think you should try $("span.current-stage").html(''); or you can also use .append method .

Comments

0

Note that you can use only CSS for the same result :

ul.status li.blue span.current-stage {
  display: inline-block;
}
ul.status li span.current-stage {
  display: none;
}

2 Comments

So how would i show it and inject my image? That will just not display anything.
With this solution, you don't need jQuery code. The CSS automatically hides the span.current-stage when the li doesn't have the blue class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.