Solved

can someone please see what is wrong with my Jquery

Posted on 2012-04-13
4
268 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

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…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now