Checkbox value and text? 
Author Message
 Checkbox value and text?

Hi,

In an HTML form I need to capture both the value and displayed text of a
checkbox control? How do I do this?

Example:
    <INPUT name=checkbox1 value="12345">Checked Item</INPUT>

I want to grab both the value "12345" and "Checked Item".

Thanks.

Brian Armstrong.



Tue, 16 Jul 2002 03:00:00 GMT  
 Checkbox value and text?

checkbox1.value
checkbox1.innerText

--
Michael Harris


  Hi,

  In an HTML form I need to capture both the value and displayed text of a
  checkbox control? How do I do this?

  Example:
      <INPUT name=checkbox1 value="12345">Checked Item</INPUT>

  I want to grab both the value "12345" and "Checked Item".

  Thanks.

  Brian Armstrong.



Tue, 16 Jul 2002 03:00:00 GMT  
 Checkbox value and text?

Hi,

The .value property works but .innerText returns a blank.

Anything I'm doing wrong?

Here is my code:

  for ( var i = 0;i < form.chkRowItem.length; i++ ) {
    if ( form.chkRowItem[i].checked == "1" ) {
       alert(form.chkRowItem[i].value);
       alert(form.chkRowItem[i].innerText);
    }
  }

Thanks.

Brian Armstrong.


  checkbox1.value
  checkbox1.innerText

  --
  Michael Harris


    Hi,

    In an HTML form I need to capture both the value and displayed text of a
    checkbox control? How do I do this?

    Example:
        <INPUT name=checkbox1 value="12345">Checked Item</INPUT>

    I want to grab both the value "12345" and "Checked Item".

    Thanks.

    Brian Armstrong.



Tue, 16 Jul 2002 03:00:00 GMT  
 Checkbox value and text?

My error...  

A checkbox is a type of <input> element which is an empty element, meaning that it doesn't have a closing tag </input>.  So there is no enclosed content, text or otherwise.   So you have to create a predictable relationship between the two.  One way (of probably many) is to enclose the text in a <span> element with an id that is related to the name of the checkbox it's related to.  Another is to add a "custom" attribute to the element that duplicates the text that follows it.  Personally I'd use the 1st method, but I thought I'd give you a choice.  

The following is an example that works in at least IE4+ (and probably NN4+ as well - I don't do NN)... The <span> has been in IE since 3.0 but not accessible from script until 4.0.

<HTML><HEAD></HEAD>
<BODY>

<!-- related by name... --->
<INPUT name=checkbox1 type=checkbox value="12345"
       onclick="alert(document.all[this.name+'text'].innerText)">
       <span id=checkbox1text>Checked Item 1</span>

<!-- custom "text" attribute --->
<INPUT name=checkbox2 type=checkbox value="12345"
       onclick="alert(this.text)"
       text="Checked Item 2">Checked Item 2

</BODY>
</HTML>

--
Michael Harris


  Hi,

  The .value property works but .innerText returns a blank.

  Anything I'm doing wrong?

  Here is my code:

    for ( var i = 0;i < form.chkRowItem.length; i++ ) {
      if ( form.chkRowItem[i].checked == "1" ) {
         alert(form.chkRowItem[i].value);
         alert(form.chkRowItem[i].innerText);
      }
    }

  Thanks.

  Brian Armstrong.


    checkbox1.value
    checkbox1.innerText

    --
    Michael Harris


      Hi,

      In an HTML form I need to capture both the value and displayed text of a
      checkbox control? How do I do this?

      Example:
          <INPUT name=checkbox1 value="12345">Checked Item</INPUT>

      I want to grab both the value "12345" and "Checked Item".

      Thanks.

      Brian Armstrong.



Tue, 16 Jul 2002 03:00:00 GMT  
 Checkbox value and text?

Hi Michael,

Thanks for your help.  I've used the 2nd example as this provides me much more flexibility than having to work to hard at getting at the values. Just for anybody's else's interest I have managed to place an array in the text value and then extract pieces of information from this array based on its ordinal position.

I am using XSL and XML and so the code behind the values are hidden. I have a function in another frame which determines if items are checked and then add thems to a list from which further processing takes place.

Works like a charm. Thanks Michael.

Brian Armstrong.


  My error...  

  A checkbox is a type of <input> element which is an empty element, meaning that it doesn't have a closing tag </input>.  So there is no enclosed content, text or otherwise.   So you have to create a predictable relationship between the two.  One way (of probably many) is to enclose the text in a <span> element with an id that is related to the name of the checkbox it's related to.  Another is to add a "custom" attribute to the element that duplicates the text that follows it.  Personally I'd use the 1st method, but I thought I'd give you a choice.  

  The following is an example that works in at least IE4+ (and probably NN4+ as well - I don't do NN)... The <span> has been in IE since 3.0 but not accessible from script until 4.0.

  <HTML><HEAD></HEAD>
  <BODY>

  <!-- related by name... --->
  <INPUT name=checkbox1 type=checkbox value="12345"
         onclick="alert(document.all[this.name+'text'].innerText)">
         <span id=checkbox1text>Checked Item 1</span>

  <!-- custom "text" attribute --->
  <INPUT name=checkbox2 type=checkbox value="12345"
         onclick="alert(this.text)"
         text="Checked Item 2">Checked Item 2

  </BODY>
  </HTML>

  --
  Michael Harris



Wed, 17 Jul 2002 03:00:00 GMT  
 Checkbox value and text?
Have you considered using a LABEL?? Seems to be the right html tag for
what you do.

<SCRIPT>
function showValueAndLabel (el) {
  var v = el.value + '; ';
  var labels = document.getElementsByTagName('LABEL');
  for (var i = 0; i < labels.length; i++)
    if (labels[i].htmlFor == el.id)
      break;
  v += document.all ? labels[i].innerHTML :
labels[i].firstChild.nodeValue;
  alert(v);

Quote:
}

</SCRIPT>
</HEAD>
<BODY>

<FORM >
<INPUT ID="cb1" TYPE="checkbox" value="Kibology"
onClick="showValueAndLabel(this)">
<LABEL FOR="cb1">
Kibology for all
</LABEL>
</FORM>

Quote:

> Hi,

> In an HTML form I need to capture both the value and displayed text of a
> checkbox control? How do I do this?

> Example:
>     <INPUT name=checkbox1 value="12345">Checked Item</INPUT>

> I want to grab both the value "12345" and "Checked Item".

> Thanks.

> Brian Armstrong.

--

        Martin Honnen

        http://home.t-online.de/home/martin.honnen/jsgoddies.html



Thu, 18 Jul 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Displaying text/input field depending on checkbox value

2. checkbox checked values in IE (ie5)

3. Validate Value of Checkbox

4. looping through checkbox values

5. checkbox value

6. Validating Value of Checkbox

7. Value de CHECKBOX

8. Checkbox values

9. checkbox values from database

10. How to get checkbox value ?

11. Request checkbox values on next page...?

12. Value of radio buttons/checkboxes

 

 
Powered by phpBB® Forum Software