1

Can anyone advise me what is wrong with the empty String check in the JavaScript below.

It works fine if I remove the line of code which checks for empty String, but I would like to display "Nothing selected" if the selection is empty.

<html>
  <script language="JavaScript">
  <!--
  function update()
  {
    var selection = "";
    var empty = "";
    if (document.form1.TeaName.checked)    selection += "Tea<br>";
    if (document.form1.CoffeeName.checked) selection += "Coffee<br>";
    if (document.form1.EggsName.checked)   selection += "Eggs<br>";
    if (document.form1.BaconName.checked)  selection += "Bacon<br>";
    if (selection.equals(empty)) selection = "Nothing selected.";
    document.getElementById("currentSelection").innerHTML = selection;
  }
  -->
  </script>
  <body>
    <form name="form1">
      <div id="container" style="width:100%">
        <div id="left" style="float:left; width: 30%;">
          <h3>List 1</h3>
          <input type="checkbox" onClick="update()" name="TeaName"    value="Tea">Tea<br>
          <input type="checkbox" onClick="update()" name="CoffeeName" value="Coffee">Coffee<br>
        </div>
        <div id="middle" style="float:left; width: 30%;">
          <h3>List 2</h3>
          <input type="checkbox" onClick="update()" name="EggsName"  value="Eggs">Eggs<br>
          <input type="checkbox" onClick="update()" name="BaconName" value="Bacon">Bacon<br>
        </div>
        <div id="right" style="float:left; width: 30%;">
          <h3>Currently selected</h3>
          <p id="currentSelection">Please make a selection.</p>
        </div>
      </div>
    </form>
  </body>
</html>

Thank you for your time,

James

5 Answers 5

3

There's no such method equals. Don't complicate things:

if (selection.length === 0) selection = "Nothing selected.";
Sign up to request clarification or add additional context in comments.

Comments

2

replace this row:

 if (selection.equals(empty)) selection = "Nothing selected.";

with this:

 if (selection == "" ) selection = "Nothing selected.";

Comments

0

Try this

if(!selection) selection="Nothing selected.";

Comments

0

There is no equals function in javascript, you either have to check with the length or a simple === operator

if (selection.length<= 0) selection = "Nothing selected.";

               (OR)

if(selection === "") selection  = "Nothing selected";

              (OR)

selection=(selection.length > 0) ? selection : "Nothing selected";

The function equals you have used should be related to some javascript library or framework.

Comments

0

You can simply have default text and skip the last check.

var selection = "Nothing selected."; //default text
// your rest of the code.

OR

Last check can be done as

selection = selection || "Nothing selected.";

Comments

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.