0

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('£',''));
}
}
9
  • The given code looks fine... Commented Jul 17, 2015 at 10:37
  • 1
    What is the syntax error you get? Commented Jul 17, 2015 at 10:37
  • @ArunPJohny, yeah, because I haven't put the function inside the IF statement. Commented Jul 17, 2015 at 10:38
  • which function? Show the code where you get the error. Commented Jul 17, 2015 at 10:38
  • 2
    You seems missing a } to close the if Move the } at the most bottom after $('#myCont').html(sorted); Commented Jul 17, 2015 at 10:40

3 Answers 3

5

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!
}
}
Sign up to request clarification or add additional context in comments.

Comments

2

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);
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.