2

I'm trying to get the #percent amount to change to the text contained in the li that is clicked. Someone help me fix my code please and thanks!

<div id="percentchooser">
<ul>
    <li>25</li>
    <li>30</li>
    <li class="selectedpercentage">35</li>
    <li>40</li>
    <li>45</li>
    <li>50</li>
</ul>
<div id="percentage"><span id="percentamount">35</span><span id="percentsign">%</span>

</div>

$("#percentchooser li").click(function () {

    var percentage = $(this).html();

    $("#percentchooser li").removeClass("selectedpercentage");
    $(this).addClass("selectedpercentage");

    $("#percentageamount").html(percentage);


});
2
  • That should work fine. Horrifically inefficiently, but otherwise fine. Commented Jan 29, 2013 at 23:02
  • See jQuery .text(): api.jquery.com/text/#text2 Commented Jan 29, 2013 at 23:02

3 Answers 3

3

You seem to reference the wrong id, the id of the span is percentamount in your HTML example but you are referencing it as percentageamount in the script:

$("#percentchooser li").click(function () {
    var percentage = $(this).html();

    $("#percentchooser li").removeClass("selectedpercentage");
    $(this).addClass("selectedpercentage");

    $("#percentamount").html(percentage);
});

Ones changed it seems to work.


DEMO - Using #percentamount instead


Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it always helps having someone else look at it. silly mistake.. I thought I knew what I was doing..
0

i dont see any percentage amount id here, so i guess this should work

   $("#percentamount").html(percentage);

Comments

0

Have you considered not using jQuery? It is unnecessary in this situation:

(function() {
  var chooser = document.getElementById('percentchooser'),
      selected = (function() {
        var li = chooser.children[0].children, len = li.length, i;
        for(i=0;i<l;i++) if(li[i].className == "selectedpercentage") return li[i];
      })(), target = document.getElementById('percentamount');
  chooser.onclick = function(e) {
    e = e || window.event;
    var t = e.srcElement || e.target;
    if( !t.tagName) t = t.parentNode; 
    if( t.tagName.toLowerCase() == "li") {
      selected.className = "";
      t.className = "selectedpercentage";
      target.firstChild.nodeValue = t.firstChild.nodeValue;
    }
  };
})();

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.