Solved

can someone please see what is wrong with my Jquery

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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)

839 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