shanish
shanish

Reputation: 1984

compare with jQuery

I have an array called billItems in my jquery, and am having values in that like,

[TUTION FEE ,SPORTS FEE ,CONVOCATION FEE ]

am iterating the billItems before adding new item, to avoid duplicate, like

for (var i = 0; i < billItems.length; i++) {
    var name = billItems[i];

    if (name == feesItem.ItemName) {  //here ItemName=TUTION FEE
        var validItem = 'False'
    }
}

as I mentioned above, am passing the ItemName as TUTION FEE, but its not comparing the strings and the control not comes into the block even its same. whats wrong with this query, can anyone help me here....

am just appending the datas into a html table like

var feesItem = new Object();

  feesItem.ItemName     = $("#txtItemName").val();
  feesItem.Amount       = $("#txtAmount").val();

$("#BillTable > tbody").find("tr:gt(0)").remove();
  for (var i = 0; i < feesItemList.length; i++) {  
tab = "<tr><td>" + " <span class='bid'>" + feesItem[i].ItemName + " </span>
       </td><td>" + feesItem[i].Amount + "</td></tr>";

          $("#BillTable > tbody").append(tab);
}

its still not comparing the values...

I just tried comparing the values within the for loop like,

 var billItems = [];
  $(".bid").each(function () {
      billItems.push($(this).text());
  });

 var validItem = ($.inArray(feesItem[i].ItemName, billItems) < 0);

Upvotes: 0

Views: 146

Answers (5)

Ja͢ck
Ja͢ck

Reputation: 173562

I'm going to go on a limb here and guess that your strings are not exactly what they seem. Try changing the comparison to:

if ($.trim(name) == $.trim(feesItem.ItemName)) {

This will remove any trailing or leading spaces from both arguments before comparison. Hope that helps.

Upvotes: 1

Alnitak
Alnitak

Reputation: 339816

Use $.inArray():

var billItems = ['TUITION FEE', 'SPORTS FEE', 'CONVOCATION FEE'];
var validItem = ($.inArray(ItemName, billItems) < 0);

Upvotes: 3

BorisD
BorisD

Reputation: 2261

Try this:

validItem = true;

for(var i = 0; i < billItems.length; i++) {
    var name = billItems[i];

    if(name == ItemName) { 
       validItem = false;
       break;
    }
}

if(validItem){
  billItems.push(ItemName);
}

Upvotes: 1

heads5150
heads5150

Reputation: 7443

Take note of @Rory McCrossan comment about var validItem = false

Also ensure you are comparing string vs string or int vs int javascript can give some false-y vales when cross comparing value types.

As an aside the way to rewrite your code is

//Search for a specified value within an array and return its index (or -1 if not found).
var index = jQuery.inArray(ItemName, billItems);

var validItem = index > -1;

Upvotes: 1

Patricia
Patricia

Reputation: 7802

You can use jQuery's inArray function for this:

Here's the documentation: http://api.jquery.com/jQuery.inArray/

and here's some code to show you how to use it:

var myArray = ['TUTION FEE' ,'SPORTS FEE' ,'CONVOCATION FEE' ];
var itemName = 'SPORTS FEE';
var nonExistentItemName = 'OTHER FEE';
var isValid = false;

if($.inArray(itemName, myArray) == -1){
    isValid = true;
}

alert(isValid);

isValid = false;

if($.inArray(nonExistentItemName, myArray) == -1){
    isValid = true;
}

alert(isValid);

and here is the jsFiddle to demonstrate it: http://jsfiddle.net/sPeem/

Upvotes: 1

Related Questions