2

I have 4 href's which when clicked on i need to change the class of a div.

For example: - Click on link 1 = the div class will change to s1, - Click on link 2 = the div class will change to s2, - Click on link 3 = the div class will change to s3,

And so on, I have some code below, but not sure on the jQuery as im just learning it

http://jsfiddle.net/Nh7tn/1/

<div class="vehicleJourney">
    <div id="progress">
        <div id="complete" class="s2"><!--need class to change -->
            <div id="marker"></div>
        </div>
    </div>

    <div class="buttons">
      <ul>
        <li class="s1"><a  class="showSingle" data-target="1">s1</a></li>
        <li class="s2"><a  class="showSingle selected" data-target="2">s2</a></li>
        <li class="s3"><a  class="showSingle" data-target="3">s3</a></li>
        <li class="s4"><a  class="showSingle" data-target="4">s4</a></li>
      </ul>
    </div>

</div>​

Cheers

4 Answers 4

3

If you want to apply to the div the class in the text of the link, you may do this :

$('a.showSingle').click(function(){
   $('#complete').attr('class', $(this).text()); 
});

Demonstration

If you want to apply to the div the class of the parent of the link, you may do this :

$('a.showSingle').click(function(){
   $('#complete').attr('class', $(this).parent().attr('class')); 
});

Demonstration

If you want to apply to the div a class computed from the number of the link, you may do this :

$('a.showSingle').click(function(){
   $('#complete').attr('class', 's'+($(this).parent().index()+1)); 
});

Demonstration

When you're in your real application (i.e. not in jsfiddle), the code should be embedded in a ready callback in a script at the end of your body :

<script>
    $(function(){
       $('a.showSingle').click(function(){
          $('#complete').attr('class', 's'+($(this).parent().index()+1)); 
       });
    });
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

cheers, the text within <a class="showSingle" data-target="1">s1</a></li> reads something else, i just put that in there for demo purposes
0

Try this:

$(document)​.ready(function() {
    $('.showSingle').click(function() {
        $('#complete').attr('class', '').addClass('s' + $(this).data('target'));
    });
});​

Comments

0

See the updated jsfiddle

$('.showSingle').click(function(){$('#complete').attr('class',$(this).html());});

Comments

0

That should work

$("li a").click(function(){
  var classname=$(this).text();
  $("#complete").attr("class",classname);
});

Comments

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.