H.HISTORY
H.HISTORY

Reputation: 518

execute jquery function inside an IF statement?

I'm trying to execute a jquery function inside an IF statement.

basically, I am getting the value of a select option and if the value of selected option is what I want (htl in this example) then I want to execute the function!

but when I wrap the code within an IF statement, i get syntax error which I have no idea what's causing that issue.

This is my entire code:

var ascending = false;
$('.page_navigation .sortBy').change(function () {    
    var vals = $(this).val();
    if (vals == "htl") {    
        ///// I need to put the code bellow here    
    }
    var sorted = $('.mypro').sort(function (a, b) {
        return (ascending == (convertToNumber($(a).find('.prod-price').html()) < 
                  convertToNumber($(b).find('.prod-price').html()))) ? 1 : -1;
    });
    ascending = ascending ? false : true;

    $('#myCont').html(sorted);
});
var convertToNumber = function (value) {
    return parseFloat(value.replace('£', ''));
}

could someone please advise on this issue?

I tried this and I get the syntax error:

var ascending = false;

$('.page_navigation .sortBy').change(function(){

    var vals = $(this).val();
    if(vals == "htl") {


    var sorted = $('.mypro').sort(function(a,b){
        return (ascending ==
               (convertToNumber($(a).find('.prod-price').html()) < 
                convertToNumber($(b).find('.prod-price').html()))) ? 1 : -1;
    });
    ascending = ascending ? false : true;

    $('#myCont').html(sorted);
});
var convertToNumber = function(value){
     return parseFloat(value.replace('£',''));
}
}

Upvotes: 0

Views: 2175

Answers (3)

andrew
andrew

Reputation: 9583

This is exactly why you should be coding with an IDE and not in a text editor

Syntax errors will be highlighted instantly and you will save yourself a lot of time

take a look at the screenshot from netbeans:

enter image description here

Upvotes: 0

Tushar Gupta
Tushar Gupta

Reputation: 15923

Limit the scope of your if block to $('#myCont').html(sorted); ie

if(vals == "htl") {
    var sorted = $('.mypro').sort(function(a,b){
        return (ascending ==
               (convertToNumber($(a).find('.prod-price').html()) < 
                convertToNumber($(b).find('.prod-price').html()))) ? 1 : -1;
    });
    ascending = ascending ? false : true;
    $('#myCont').html(sorted);
}

Upvotes: 2

Liam
Liam

Reputation: 29694

Your closing the if wrong. You need to put the closing brace (of the if statement) inside the function call, so:

var ascending = false;

$('.page_navigation .sortBy').change(function(){

    var vals = $(this).val();
    //closing brace matches this opening one
    if(vals == "htl") {    
          var sorted = $('.mypro').sort(function(a,b){
          return (ascending ==
               (convertToNumber($(a).find('.prod-price').html()) < 
                convertToNumber($(b).find('.prod-price').html()))) ? 1 : -1;
       });
       ascending = ascending ? false : true;

       $('#myCont').html(sorted);
   //put the brace inside the function...i.e. close the if brace
   }
});
var convertToNumber = function(value){
     return parseFloat(value.replace('£',''));
}

The incorrect bit is:

var convertToNumber = function(value){
     return parseFloat(value.replace('£',''));
//WHY's this brace here!
}
}

Upvotes: 5

Related Questions