0
var vettore = document.getElementById(id_form).elements;


for (var i = 0; i < vettore.length; i++)
   {
    if (vettore[i].checked)
            {
            contatore++;

            valore_corrente = document.getElementById.elements[i].value;

            stringaFileSelezionati+= valore_corrente;

            stringaFileSelezionati+= '?';
            }
   }

  if (contatore == 0) 
{
alert('Error!!! No file selected!');
return false;
}
 else 
{
    alert(stringaFileSelezionati);

}

The error is in the line:

valore_corrente = document.getElementById.elements[i].value;

how can i get that value?

EDITED: Maybe the error is in the checkboxes creation since I got undefined:

                                    cell1.innerHTML = '<input type="checkbox" name="' + 'checkbox'+" value=" + vettore_nomi_file[i] + '" id="' +i+ '" />'+vettore_nomi_file[i];
4
  • id's should be unique on the page. So getElementById should only ever return one element. If this isn't the case your markup is technically invalid. Commented Dec 17, 2013 at 10:51
  • 1
    u know document.getElementById needs a parameter right? Commented Dec 17, 2013 at 10:51
  • @AndréJunges right, you need to read the docs on getElementById Commented Dec 17, 2013 at 10:53
  • It's the ID of a form and with elements I take all it's child elements: all it's checkboxe Commented Dec 17, 2013 at 10:59

2 Answers 2

2

You retrieve .value from the same object you retrieved .checked from:

valore_corrente = vettore[i].value;

The object is an HTMLInputElement instance. The value of the checkbox is reflected in the value property, just like whether it's checked is reflected in the checked property.

Here's a complete, working example: Live Copy | Source

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Example</title>
</head>
<body>
  <form id="theform">
    <label><input type="checkbox" value="1" checked>Uno</label>
    <br><label><input type="checkbox" value="2" checked>Due</label>
    <br><label><input type="checkbox" value="3">Tre</label>
    <br><input type="button" id="thebutton" value="Click">
  </form>
  <script>
    document.getElementById("thebutton").onclick = function() {
      var id_form = "theform";
      var vettore = document.getElementById(id_form).elements;

      var contatore = 0;
      var valore_corrente;
      var stringaFileSelezionati = "";

      for (var i = 0; i < vettore.length; i++)
      {
        if (vettore[i].checked)
        {
          contatore++;

          valore_corrente = vettore[i].value;

          stringaFileSelezionati+= valore_corrente;

          stringaFileSelezionati+= '?';
        }
      }

      if (contatore == 0) 
      {
        alert('Error!!! No file selected!');
        return false;
      }
      else 
      {
        alert(stringaFileSelezionati);

      }

    };
  </script>
</body>
</html>
Sign up to request clarification or add additional context in comments.

9 Comments

With valore_corrente = vettore[i].value; I get error: undefinedon? (this was my first try)
@newtphp: It works for me if I declare variables. Note that in your code, you're not declaring several variables, and in one case (stringaFileSelezionati) you're trying to use the value of it without having either declared it or given it a value. If you just assign to something you haven't declared, you fall prey to The Horror of Implicit Globals and it seems to work. But if you try to read a value from something you haven't declared, that's a runtime error.
@newtphp: Well, I don't know what to tell you. If vettore[i].checked is true, vettore[i].value will not be undefined. It can be a blank string, but not undefined. And as you can see from the example, it does work.
@newtphp: That seems unlikely.
@newtphp: If you're seeing "on", that means you're not succeeding in setting the value attribute of the input element. The default value for a checkbox is "on" (when checked). The reason for that is your code for setting the cell's content messes up its quotes. This fixes those quotes: cell1.innerHTML = '<input type="checkbox" name="checkbox" value="' + vettore_nomi_file[i] + '" id="' +i+ '" />'+vettore_nomi_file[i]; But note that using an id value starting with a digit will lead to trouble with CSS selectors (it's fine otherwise).
|
1

I believe this should work:

valore_corrente = vettore[i].value;

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.