0

I need to access to a value in a checkbox, the atribute value has content, so i need to place the id somewhere else i created a label, but i have not access to that value

alert(check[i].label); // doesnt work 

where else can i place a value in checkbox. Please dont write that i can do this

  <input type='checkbox' id='bla' name='mybla' vlaue='myvalue'> Hy

Where can i place some other values ?

I tryed with this

<input type='checkbox' id='bla' name='mybla' vlaue='myvalue' label='myothervalue'> Hy

first i get all checkbox ect... and in the for loop i did this

alert(check[i].label); // doesnt work 

How can i do that?

Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
streetparade
  • 32,000
  • 37
  • 101
  • 123

2 Answers2

4

It is indeed possible to store the extra data as a custom attribute on the <input> element. When you want to read the value, you can do it like this:

alert(check[i].getAttribute('label'));

Since you have tagged the question jQuery, here's the trendy version:

alert($(check[1]).attr('label'));

See these discussions if you are woried about using custom HTML attributes.

Community
  • 1
  • 1
Jørn Schou-Rode
  • 37,718
  • 15
  • 88
  • 122
0

When I am being tempted to do this, I prefer to use a related hidden element to store the "other value". It feels cleaner to me, and I also don't have to wade through the validation warnings that otherwise could come at me.
Is that something you can use here?

npup
  • 2,531
  • 1
  • 17
  • 22