Solved

can someone please see what is wrong with my Jquery

Posted on 2012-04-13
4
277 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
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…

729 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