Solved

can someone please see what is wrong with my Jquery

Posted on 2012-04-13
4
273 Views
Last Modified: 2012-04-14
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
Comment
Question by:jecommera
  • 2
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
macarrillo1 earned 500 total points
ID: 37843562
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
 

Author Comment

by:jecommera
ID: 37846090
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
 
LVL 9

Expert Comment

by:macarrillo1
ID: 37846262
try:

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

It might be a problem with your parenthesis.
parseInt(  ('.mnumber').text()   )
0
 

Author Comment

by:jecommera
ID: 37846266
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Angular 2 complaining about map file 7 86
Animated .jpg? 13 80
Clear single text form value in jquery 2 37
JQuery JavaScripot loop though RadioButtonList 4 25
I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question