3 Replies - 1102 Views - Last Post: 20 December 2014 - 09:36 AM

#1 cantreallythinkofit   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 20-December 14

If statement not working (JSON / JQuery)

Posted 20 December 2014 - 07:29 AM

I am trying to create a search form where when the user chooses a certain thing from the drop down menus and clicks search the objects that relate to that search will be displayed.To do this i'm using JSON and JQuery. Here's the JSON and JQuery/javascipt:

var data= {  
    "properties":[  
        {  
            "id":"prop1",
            "type":"House",
            "bedrooms":3,
            "price":650000,
            "tenure":"Freehold",
            "description":"Attractive three bedroom house",
            "location":"Petts Wood Road, Petts Wood, Orpington",
            "picture":"images/prop1pic1small.jpg",
            "url":"properties/prop1.html",
            "added":{  
                "month":"January",
                "day":12,
                "year":2014
            }
        },
        {  
            "id":"prop2",
            "type":"Flat",
            "bedrooms":2,
            "price":299995,
            "tenure":"Freehold",
            "description":"Presented in excellent decorative order.",
            "location":"Crofton Road Orpington BR6",
            "picture":"images/prop2pic1small.jpg",
            "url":"properties/prop2.html",
            "added":{  
                "month":"September",
                "day":14,
                "year":2014
            }
        }
    ]
}





<script>
    $("#search").on("click", function() {

        var userHousing = $("option:checked").val();
        var userLocation = $("option:checked").val();


        var output = "<ul>";

        for (var i in data.properties) {
            
            /* if the type of house and location chosen is equal to what is in the properties object */
            if ((userHousing == data.properties[i].type) && (userLocation == data.properties[i].location)) {

                output += "<li>" + data.properties[i].id + data.properties[i].added.month + " " + data.properties[i].type + data.properties[i].bedrooms + "</li>";


            }


        }


        output += "</ul>";
        document.getElementById("placeholder").innerHTML = output;

    });
</script>




The problem is that the if statement is not working. I want the results to be displayed if the type of house and location chosen is the same as the ones recorded in the properties object. I tried displaying the house and location seperately( code for this is shown below) and it worked but i just can't seem to get the if statement to display if both house and location is the same as the ones recorded in the properties object. I don't know what i am doing wrong. If anyone can help point out what's wrong with my code it'll be a great help.

 for (var i in data.properties) {

            if (userHousing == data.properties[i].type) {

                output += "<li>" + data.properties[i].id + data.properties[i].added.month + " " + data.properties[i].type + data.properties[i].bedrooms + "</li>";

            }
          
            if (userLocation == data.properties[i].location) {

                output += "<li>" + data.properties[i].id + data.properties[i].added.month + " " + data.properties[i].type + data.properties[i].bedrooms + "</li>";


            }


        }



Is This A Good Question/Topic? 0
  • +

Replies To: If statement not working (JSON / JQuery)

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: If statement not working (JSON / JQuery)

Posted 20 December 2014 - 08:59 AM

Moved to jQuery forum.
Was This Post Helpful? 0
  • +
  • -

#3 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1578
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: If statement not working (JSON / JQuery)

Posted 20 December 2014 - 09:04 AM

Aren't you getting the same value for both userHousing and userLocation? :)

You are fetching the values for userHousing and userLocation from the same element
The two variables will therefore have the same value
var userHousing = $("option:checked").val();
var userLocation = $("option:checked").val();


You sure must be having the values in two separate elements, right?
Was This Post Helpful? 1
  • +
  • -

#4 cantreallythinkofit   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 20-December 14

Re: If statement not working (JSON / JQuery)

Posted 20 December 2014 - 09:36 AM

Ahh yeah i figured that out just now aswell i thought that userLocation was working perfectly before lol. Thanks for the help anyway

This post has been edited by Dormilich: 20 December 2014 - 09:43 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1