Get active li value in ul

I am trying to get the active li from ul in jqvalue, but all I get is null. I also tried writing the ul value using the console dir (to try to simulate php var_dump, but nothing happens there

Here is my jfidde: http://jsfiddle.net/#&togetherjs=MDbkUMciB7

Here is the code

function monthly_payment(){
    var interest = $('ul.credit').find('li.active').data('interest');
    console.dir($('ul.credit'));
alert(interest);
      // var iinterest   = interest_rate / 1200;


}


<ul id="credit">
<li interest = "0.056" class="active"><a href="#"><span id="excellent" class="0.056">excellent</span></a></li>
<li interest = "0.099"><a href="#"><span id="good" class="0.099">good</span></a></li>                       <li interest = "0.169"a href="#"><span id="fair" class="0.169">fair</span></a></li>
<li interest = 0.299><a href="#"><span id="bad" class="0.299">bad</span></a></li>
</ul>
+4
source share
3 answers

Change the class to ID ie li.creditto li#creditand interesttodata-interest

HTML

<ul id="credit" name="credit">
            <li data-interest = "0.056" class="active"><a href="#"><span id="excellent" class="0.056">excellent</span></a></li>
            <li data-interest = "0.099"><a href="#"><span id="good" class="0.099">good</span></a></li>                      
           <li data-interest = "0.169"><a href="#"><span id="fair" class="0.169">fair</span></a></li>
            <li data-interest = 0.299><a href="#"><span id="bad" class="0.299">bad</span></a></li>
        </ul>

JQuery

$(function() {
    monthly_payment();
 });


function monthly_payment(){
    var interest = $('ul#credit').find('li.active').data('interest');
    alert(interest);
   console.dir($('ul.credit'));
       var iinterest   = interest_rate / 1200;
       payment = principal * interest / (1 - (Math.pow(1/(1 + interest), monthly_period)));
       payment = Math.round(payment * 100) / 100;
       return payment;

}
+5
source

If you want to use data attributes, then prefix it data-

Your HTML should be:

<ul id="credit">
<li data-interest = "0.056" class="active"><a href="#"><span id="excellent" class="0.056">excellent</span></a></li>
<li data-interest = "0.099"><a href="#"><span id="good" class="0.099">good</span></a></li>                       <li data-interest = "0.169"a href="#"><span id="fair" class="0.169">fair</span></a></li>
<li data-interest = 0.299><a href="#"><span id="bad" class="0.299">bad</span></a></li>
</ul>

And, as mplungjan said, class names cannot begin with numbers. It should begin with the alphabet.

.1{
 /* bad */
}

.some1{
 /* good */
}
+1

var interest = $('ul#credit').find('li.active').attr('interest');

you have a "credit" as id in your html.

-1
source

Source: https://habr.com/ru/post/1535222/


All Articles