please see what is wrong with my collection of jquery each for div changes

jecommera
jecommera used Ask the Experts™
on
Hi,

Please see what is wrong with my jquery which changes a class of divs content

$(document).ready(function(){

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

      function convert(metric) {

            switch(metric){
                 case 'cm':
                       $(function(){
                      $.each('.mnumber',function() {$(this).html(Math.round(parseInt($('.mnumber').text())*2.54)+" cm");})});
                      break;
                 case 'in':
                      $(function(){
                      $.each('.mnumber',function() {$(this).html(Math.round(parseInt($('.mnumber').text())*0.39)+" in");})});
                      break;
            }
      }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014

Commented:
what about :
$(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() {$(this).html(Math.round(parseInt($('.mnumber').text())*2.54)+" cm");});
                      break;
                 case 'in':
                      $.each('.mnumber',function() {$(this).html(Math.round(parseInt($('.mnumber').text())*0.39)+" in");});
                      break;
            }
      }

Open in new window

Author

Commented:
Sorry I updated your code but the div contents do not change
leakim971Multitechnician
Top Expert 2014

Commented:
please provide HTML code of the div

Author

Commented:
Please find below:

<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() {$(this).html(Math.round(parseInt($('.mnumber').text())*2.54)+" cm");});
                      break;
                 case 'in':
                      $.each('.mnumber',function() {$(this).html(Math.round(parseInt($('.mnumber').text())*0.39)+" in");});
                      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>
leakim971Multitechnician
Top Expert 2014

Commented:
try this :

$(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() {
                          $(this).html(Math.round(parseInt($('.mnumber').text())*2.54)+" cm");
                      });
                      break;
                 case 'in':
                      $.each('.mnumber',function() {
                          $(this).html(Math.round(parseInt($('.mnumber').text())*0.39)+" in");
                      });
                      break;
            }
      }
})¿

Open in new window

Author

Commented:
sorry - no luck
leakim971Multitechnician
Top Expert 2014

Commented:
replace :
$.each('.mnumber',function() {
by :
$('.mnumber').each(function() {

Author

Commented:
I get error

".mnumber".text is not a function
[Break On This Error]       

$('.mnumber').html(parseInt(('.mnumber').text())*2.54);
Multitechnician
Top Expert 2014
Commented:
replace by :
$(this).html(parseInt($(this).text())*2.54);

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial