Print the value?

Hi guys,

I’m trying to print the value of each hidden field.
Here is what I did so far,
HTML


<tbody>
            <tr id="1"><td>1</td>	<td>One</td>	<input type="hidden" name="lesson" id="lessonid33" value="1" /></tr>
	    <tr id="2"><td>2</td>	<td>Two</td>	<input type="hidden" name="lesson" id="lessonid34" value="2" /></tr>
	    <tr id="3"><td>3</td>	<td>Three</td>	<input type="hidden" name="lesson" id="lessonid11" value="3" /></tr>
	    <tr id="4"><td>4</td>	<td>Four</td>	<input type="hidden" name="lesson" id="lessonid21" value="4" /></tr>
</tbody>

JS


$(document).ready(function() {
	function reassignIds(){
	  var length = 1;
		
	  $('#table-1 > tbody tr').val().each(function() {
		$(this).children(":first").text(length);
	    length++;
	  })
	}
		
	$("#table-1").tableDnD({
		onDrop: function(table, row) {
            $('tr input:hidden', table).val().text();
        }
	});
    
});

What’s the proper codes to print it?

Thanks in advance.

Hi there,

Storing data in a hidden field like this probably isn’t the best approach to whatever it is you are trying to do.
Why not store it as a data attribute on the <tr>

e.g.

<tr id="1"><td>1</td>	<td>One</td>	<input type="hidden" name="lesson" id="lessonid33" value="1" /></tr>

becomes:

<tr id="1" data-lesson-id="lessonid33" data-lesson-value="1"><td>1</td><td>One</td></tr>

Then you can do:

console.log($("#1").data("lesson-id");

BTW, it is best not to have your ids start with numbers.
According to the HTML5 specification anything but a space is okay, but I would still avoid it.
http://stackoverflow.com/questions/5672903/can-i-have-a-div-with-id-as-number