0

I am trying to select and return some items(checkbox). Everything is okay, but the first item is always showing UNDEFINED. can't fix this problem!

My code is as below

function checkList () {
  var checked;
  var i;

  for( i = 0; i < document.form1.length; i++) {
    var element = document.form1[i]
    if (element.type == "checkbox"){
      if (element.checked == true){
        checked = checked + element.value + "<br/>";

      }

    }

  }
  document.getElementById('checked').innerHTML = checked;
}
<form name="form1">
  <input type="checkbox" name="checkbox1" id="checkbox1" value="Pen" />
  <label value="Earned" for="checkbox1">Pen</label>
  <br	/>
  <input type="checkbox" name="checkbox2" id="checkbox2" value="Book" />
  <label value="Earned" for="checkbox2" >Book</label>
  <br/>
  <input type="checkbox" name="checkbox1" id="checkbox3" value="Sharpner" />
  <label value="Earned" for="checkbox3">Sharpner</label>
  <br/>
  <input type="checkbox" id="checkbox4" name="checkbox1" value="Pencil" />
  <label value="Earned" for="checkbox4">Pencil</label>
  <br/> <br/>
  <input type="button" id="done" value="Done" onclick="checkList()" />
  <br/><br/>
</form>

<p >You are taking:</p>
<span id="checked"></span>

2
  • 1
    Duplicate name attribute on checkbox1 Commented Jun 9, 2015 at 11:07
  • Get your checkbox names and id's sorted out first and try again. Commented Jun 9, 2015 at 11:14

4 Answers 4

2

Try to Change

<form name="form1">
  <input type="checkbox" name="checkbox1" id="checkbox1" value="Pen" />
  <label value="Earned" for="checkbox1">Pen</label>
  <br   />
  <input type="checkbox" name="checkbox2" id="checkbox2" value="Book" />
  <label value="Earned" for="checkbox2" for="checkbox">Book</label>
  <br/>
  <input type="checkbox" name="checkbox1" id="checkbox3" value="Sharpner" />
  <label value="Earned" for="checkbox3" for="checkbox" for="checkbox">Sharpner</label>
  <br/>
  <input type="checkbox" id="checkbox4" name="checkbox1" value="Pencil" />
  <label value="Earned" for="checkbox4">Pencil</label>
  <br/> <br/>
  <input type="button" id="done" value="Done" onclick="checkList()" />
  <br/><br/>
</form>

<p >You are taking:</p>
<span id="checked"></span>

to This....

    <form name="form1">
  <input type="checkbox" name="checkbox1" id="checkbox1" value="Pen" />
  <label value="Earned" for="checkbox1">Pen</label>
  <br   />
  <input type="checkbox" name="checkbox2" id="checkbox2" value="Book" />
  <label value="Earned" for="checkbox2">Book</label>
  <br/>
  <input type="checkbox" name="checkbox3" id="checkbox3" value="Sharpner" />
  <label value="Earned" for="checkbox">Sharpner</label>
  <br/>
  <input type="checkbox" id="checkbox4" name="checkbox4" value="Pencil" />
  <label value="Earned" for="checkbox4">Pencil</label>
  <br/> <br/>
  <input type="button" id="done" value="Done" onclick="checkList()" />
  <br/><br/>
</form>

<p >You are taking:</p>
<span id="checked"></span>

You have multiple duplications within the code such as (for="")

Let me know, Thanks

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

4 Comments

What did you change and why? Explain please.
Robert, In the OP he has several duplications of for="" in each label and that looked like it could have been causing confusion. Im still learning myself so was just a thought. :)
@Birdy That was my mistake.
@Birdy: don't explain to me, I saw that. It's more for you to make a better answer ;)
1

Change

 var checked;  // like this, checked is undefined

to

 var checked = ""; // now it's simply empty

here's a fiddle http://jsfiddle.net/sq9938b0/

Comments

0

The issue is that you initialize checked variable as undefined. When you append the string to undefined it is converted to string "undefined", hence it appearing at the beginning.

Just initialize it as an empty string: var checked = "";

function checkList () {
  var checked = "";
  var i;

  for( i = 0; i < document.form1.length; i++) {
    var element = document.form1[i]
    if (element.type == "checkbox"){
      if (element.checked == true){
        checked = checked + element.value + "<br/>";

      }

    }

  }
  document.getElementById('checked').innerHTML = checked;
}
<form name="form1">
  <input type="checkbox" name="checkbox1" id="checkbox1" value="Pen" />
  <label value="Earned" for="checkbox1">Pen</label>
  <br	/>
  <input type="checkbox" name="checkbox2" id="checkbox2" value="Book" />
  <label value="Earned" for="checkbox2" for="checkbox">Book</label>
  <br/>
  <input type="checkbox" name="checkbox1" id="checkbox3" value="Sharpner" />
  <label value="Earned" for="checkbox3" for="checkbox" for="checkbox">Sharpner</label>
  <br/>
  <input type="checkbox" id="checkbox4" name="checkbox1" value="Pencil" />
  <label value="Earned" for="checkbox4">Pencil</label>
  <br/> <br/>
  <input type="button" id="done" value="Done" onclick="checkList()" />
  <br/><br/>
</form>

<p >You are taking:</p>
<span id="checked"></span>

Comments

0

Take a look of the following step

checked = checked + element.value + "
";

In the first loop you have not defined checked. So first time it is

checked = undefined + pen

Intialize

var checked = ''

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.