user1212520 user1212520 - 1 month ago 6
jQuery Question

Assigning two values to a checkbox javascript

I need to pass two different values back as the result from one checkbox. I tried an object, but the result was undefined (the code works with one value).

' <input type="checkbox" id="cat" value="'+{value1: data.blue, value2: data.red} +'"/>'


result.value1 is always undefined, whereas result works for just a string.

What is the best way to do this using javascript/jquery.

Answer

You can use attr for add more values, for example:

<input type="checkbox" id="cat" data-value1="blue" data-value2="red" value=""/>

Get values using jQuery, use:

$("#cat").data("value1");//return 'blue'
$("#cat").data("value2");//return 'red'

Result: https://jsfiddle.net/cmedina/a0ya5bwp/

Comments