Vrankela
Vrankela

Reputation: 1202

How to check if two java script variables differ?

So I have this web grid in my view:

@{
    var grid = new WebGrid(Model);
}
<div id="gridContent">

    <button type="button" id="createNewPredefinedView" style="margin-bottom: 20px;">@Html.Localize("createNew")</button>

    @grid.GetHtml(
mode: WebGridPagerModes.All,
columns:

    grid.Columns(
        grid.Column("PredefID", format: @<text>  <span class="display-mode" id="PredefViewID">@item.PredefineViewID </span> <label id="PredefID" class="edit-mode">@item.PredefineViewID</label> </text>, style: "col1Width"),


        grid.Column("", format: @<text>
        <img class="edit-table display-mode click_images" src="~/Images/edit.png" />
        <img class="delete-table display-mode click_images" src="~/Images/delete.png" />
        <img class="open-sensors display-mode click_images" src="~/Images/Sensor.png" />
        <img class="save-table edit-mode click_images" src="~/Images/save.png" />
        <img class="cancel-table edit-mode click_images" src="~/Images/cancel.png" />
        </text>, style: "col2Width", canSort: false),



        grid.Column("AmountOfSensors", @Html.Localize("amountOfSensors").ToString(), format: @<text>  <span class="display-mode" id="lblAmountOfSensors">  @item.SensorNo </span> <label id="AmountOfSensors" class="edit-mode"> @item.SensorNo </label> </text>, style: "col2Width", canSort: false)
     ))
</div>

And using this java script I want to check if var sensorNumber is greater than zero, but since these are strings, I just want to see that it is everything BUT the string "0"

$(".delete-table").on("click", function () {
    var tr = $(this).parents('tr:first');
    var sensorNumber = tr.find("#AmountOfSensors").html();
    var zero = "0"
    if ($(sensorNumber).val() !=  $(zero)) {
        alert("can't delete");
    }
    else { 
        //my delete code code goes here
    }
)};

But this way, my sensorNumber can contain the value "0".... and it will go to fulfill the if clause, the other values go to the if as well which is good, but I just want it to go to the else clause if it is a "0" and nothing else. Any ideas what I am doing wrong?

Upvotes: 1

Views: 60

Answers (2)

ekad
ekad

Reputation: 14614

$(zero) in your code means $("0") selector so it doesn't make sense to compare with $(zero). Moreover, since you have this syntax

var sensorNumber = tr.find("#AmountOfSensors").html();

then sensorNumber is actually the variable that you want to compare with "0", so you don't need to use $(sensorNumber).val(). Assuming that sensorNumber always contains an integer, set zero as an integer and use parseInt function to make sure you're comparing two integers as below

var zero = 0;
if (parseInt(sensorNumber) != parseInt(zero)) 

or don't use zero variable at all

if (parseInt(sensorNumber) != parseInt(0)) 

Upvotes: 1

Vsevolod Goloviznin
Vsevolod Goloviznin

Reputation: 12324

When you're comparing strings or numbers in JavaScript you don't need to wrap them into jQuery function. Just do a simple == check.

if ($(sensorNumber).val() !=  zero) {
   alert("can't delete");
}

Upvotes: 0

Related Questions