Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Below is my code to compare two variables:

<script>

    var to_check=$(this).val();

    var cur_string=$("#0").text();

    var to_chk = "that";

    var cur_str= "that";

    if(to_chk==cur_str){

        alert("both are equal");

        $("#0").attr("class","correct");    

    } else {

        alert("both are not equal");

        $("#0").attr("class","incorrect");

    }

</script>

But I’m getting an error. Can anyone tell me what I’m doing wrong here?

1 Answer

0 votes
by (19.7k points)

Since both the values are strings, you don’t have to compare type with ===. Check the code below:

function do_check()

{

  var str1 = $("#textbox1").val();

  var str2 = $("#textbox2").val();

  if (str1 == str2)

  {

    $(":text").removeClass("incorrect");

    alert("equal");

  }

  else

  {

    $(":text").addClass("incorrect");

    alert("not equal");

  }

}

.incorrect

{

  background: #ff8888;

}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input id="textbox1" type="text">

<input id="textbox2" type="text">

<button onclick="do_check()">check</button>

Interested in Java? Check out this Java tutorial by Intellipaat.  

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Feb 7, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...