0

HI all i have some where there is Departments and department has some data like this

          Department
           Casual shoe
           formal shoe
           sports shoe

so i want to write an if condition where i should write an if condition like this

    <script type="text/javascript">
    $(document).ready(function () {
        $.getJSON("/api/ProductLayout", function (data) {
            $.each(data, function (idx, ele) {
                $("<img/>").attr({ src: ele.ImageURL }).appendTo("#makeMeScrollable");
                $("#makeMeScrollable").append('<span>' + ele.ProductName + '</span><h4>' + ele.Price + '$' + '</h4>');
                if (ele.Department == "Shoes") {
                    alert(2);
                    //$(".img_main").empty();
                    $("<img/>").attr({ src: ele.ImageURL }).appendTo("#Watches");
                    $("#Watches").append('<span>' + ele.ProductName + '</span><h4>' + ele.Price + '$' + '</h4>');
                }
                else if (ele.Department = "Wallets") {
                    alert(3);
                    $("<img/>").attr({ src: ele.ImageURL }).appendTo("#BackPacks");
                    $("#BackPacks").append('<span>' + ele.ProductName + '</span><h4>' + ele.Price + '$' + '</h4>');
                }
                else if (ele.Department = "Belts") {
                    alert(4);
                    $("<img/>").attr({ src: ele.ImageURL }).appendTo("#Belts");
                    $("#Belts").append('<span>' + ele.ProductName + '</span><h4>' + ele.Price + '$' + '</h4>');
                }
             });
        });
    });      

</script>

now i ant to write an if condition for shoe where if (ele.Department == "Shoes") i should get all the data of deaprtments which has "shoe" in it,jow can i write an condition for this

4
  • Confused.. is Department a collection? Commented Sep 25, 2012 at 8:00
  • 1
    In your condition use "==" not "=" Commented Sep 25, 2012 at 8:02
  • @LewsTherin yes department is a collection Commented Sep 25, 2012 at 8:12
  • yes i fixed the condition use "==" not "=" Commented Sep 25, 2012 at 8:13

2 Answers 2

2

To answer you question about finding a substring..

I will try:

if(variableInQuestion.indexOf("shoes")!=-1)
{

}

And two of your if conditions use = not == you probably want to fix that.

UPDATE:

Looking at your question again..

"Formal shoes"
"Casual shoes" 

Is it possible that you want an endsWith("shoes") method? If so: endsWith in JavaScript

Sign up to request clarification or add additional context in comments.

Comments

1

I think you could use regular expressions:

this tests for shoes case insensitive. (match also "shoesprostore")

if (/shoes/i.test(ele.Department)) {
}

this tests for the word shoes case insensitive. (match "Big Shoes" and "Small Shoes") (edit 4)

if (/\bshoes\b/i.test(ele.Department)) {
}

edit:

this tests for the words shoe and shoes case insensitive.

if (/\bshoes?\b/i.test(ele.Department)) {
}

\b : start and finish of a word

? : the previous character may or may not be present

i (after the sencond /) : case insensitive

look here for more http://www.w3schools.com/jsref/jsref_obj_regexp.asp

:)

edit 2:

corrected grammar inconsistencies thanks to my friend SandMan

edit 3:

maybe you could save the regex in global scope (before the $(document).ready(...) statement) using

var shoeTest = /shoes/i; then

if (shoeTest.test(...))

2 Comments

is ##if (/shoes/i.test(ele.Department))## working with firefox 3.6 too?
yeah, tested with firefox 3.6.28

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.