SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2013
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Print the value?

    Hi guys,

    I'm trying to print the value of each hidden field.
    Here is what I did so far,
    HTML
    Code:
    <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
    Code:
    $(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.

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,941
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    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.

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

    Code:
    <tr id="1" data-lesson-id="lessonid33" data-lesson-value="1"><td>1</td><td>One</td></tr>
    Then you can do:

    Code:
    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/5...h-id-as-number


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •