Skip to main content

Trying to get value from textbox in column in table



I dynamically created a table with three columns.







<tr>

<td>1</td>

<td>SegName</td>

<td><input type='text' /></td>

</tr>







I'm trying to write a function that goes through each row and grabs the value in that will be in the textbox.





Javascript:







$("#codeSegmentBody").children().eq(x).children().eq(2).val();







The code brings brings up undefined when I do val(), but if I do html it'll grab the html of the textbox.





How can I get this to bring me the value?


Comments

  1. <table id="test">
    <tr>
    <td>
    <input type="text" value="123">
    </td>
    </tr>
    <tr>
    <td>
    <input type="text" value="abc">
    </td>
    </tr>
    </table>

    <script type="text/javascript">
    $(document).ready(function(){

    $("#test").find(":input[type=text]").each(function(){

    alert( $(this).val() );

    });

    });
    </script>


    Here is a fiddle that will get you there:

    http://jsfiddle.net/uS8AK/

    ReplyDelete
  2. Assuming #codeSegmentBody is the name of your table, try this:

    $("#codeSegmentBody td input").each(function() {
    var inputValue = $(this).val();
    alert(inputValue);
    });


    Example fiddle

    ReplyDelete
  3. $("#codeSegmentBody tr input[type='text']").each(function(){
    alert($(this).val());
    })

    ReplyDelete
  4. Try this

    $("#codeSegmentBody tr").each(function() {
    alert($(this).find("input").val());
    });

    ReplyDelete
  5. You are referencing the containing <td> not the input. Try:

    $("#codeSegmentBody").children().eq(x).find(":text").val();

    ReplyDelete
  6. var str = "";
    $("#codeSegmentBody .third-column input").each(function()
    {
    str += this.value;
    });
    alert(str);

    ReplyDelete
  7. Not easier

    $("table tr td input").val();


    ?

    BTW. You don't have any value in this input anyway.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.