Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

can someone please see what is wrong with my Jquery

Hi

Can someone see what is wrong with the jquery below:

I get an parseInt(".mnumber").text is not a function
('.mnumber').html((parseInt('.mnumber').text())*0.39);
      

$(document).ready(function(){
       $("#select_cm").click(function() {
            convert('cm');            
      });
      });
      
      $(document).ready(function(){
      $("#select_in").click(function() {
            convert('in');                  
      });
      });
      
      function convert(metric) {
            switch(metric){
            case 'cm':
            alert('hello');
            ('.mnumber').html((parseInt('.mnumber').text())*0.39);
            break;
            case 'in':
            ('.mnumber').html((parseInt('.mnumber').text())*0.39);
            break;
            }
      }
0
jecommera
Asked:
jecommera
  • 2
  • 2
1 Solution
 
macarrillo1Commented:
First, you don't need multiple document ready functions.  This might be what is causing the problem.
It also looks like you are missing the jquery($)  within you case statements.

$(document).ready(function(){

       $("#select_cm").click(function() {
            convert('cm');            
      });
     
      $("#select_in").click(function() {
            convert('in');                  
      });
      });

      function convert(metric) {

            switch(metric){
                 case 'cm':
                      alert('hello');
                      $('.mnumber').html((parseInt('.mnumber').text())*0.39);
                      break;
                 case 'in':
                      $('.mnumber').html((parseInt('.mnumber').text())*0.39);
                      break;
            }
      }
0
 
jecommeraAuthor Commented:
Hi

Thanks - that's a great help however there it still throws error:

parseInt(".mnumber").text is not a function
$('.mnumber').html((parseInt('.mnumber').text())*0.39);
0
 
macarrillo1Commented:
try:

$('.mnumber').html(parseInt(('.mnumber').text())*0.39);

It might be a problem with your parenthesis.
parseInt(  ('.mnumber').text()   )
0
 
jecommeraAuthor Commented:
Updated as follows but get not a function error

<html>
      <head>
            <title>Test</title>
       
       <script src="http://code.jquery.com/jquery-latest.js"></script>

      <script type="text/javascript">
$(document).ready(function(){

       $("#select_cm").click(function() {
            convert('cm');            
      });
     
      $("#select_in").click(function() {
            convert('in');                  
      });

      function convert(metric) {

            switch(metric){
                 case 'cm':
                      $.each('.mnumber',function() {
                          $('.mnumber').html(parseInt(('.mnumber').text())*2.54);
                      });
                      break;
                 case 'in':
                      $.each('.mnumber',function() {
                         $('.mnumber').html(parseInt(('.mnumber').text())*0.39);
                      });
                      break;
            }
      }
});
      
      
      </script>
     
</head>


<body>
<div id="size_container">
      <div id="controls">
            <span>Select metric</span>
            <form>
                  <label for="select_cm">cm</label>
                  <input type="radio" value="cm" name="metrics" id="select_cm" />
                  <label for="select_in">in</label>
                  <input type="radio" value="in" name="metrics" id="select_in" />
            </form>
           
           
      </div>
      <div class="mnumber">
      20
      </div>
     
      <div class="mnumber">
      30
      </div>
</div>
</body>
</html>
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now