0

Sorry if this is a daft question, but if I display a text input field, say your_age in a table cell, then want to dynamically display what the user inputs in the table cell next to it, how would I do that in JavaScript/jQuery?

For example if I enter 100 I want the table cell next to the input field to display 100 without the need to click a submit button.

Any help would be great. Thanks.

2
  • You are looking for the onKeyUp event. Basically every time a key is released it will fire some bit of JavaScript that can do whatever you want. Commented Aug 15, 2013 at 17:59
  • Have you analyzed your layout in terms of UX, why do you need to repeat what the user is entering in the input? Commented Aug 15, 2013 at 18:00

3 Answers 3

1
$('#myField').on('keyup', function() {
    $('#myTableCell').text($(this).val());
});

Demo: http://jsfiddle.net/seancannon/K8qZ9/7/

Sign up to request clarification or add additional context in comments.

9 Comments

.text() is usually more appropriate for user input (or any input at all)
What good would docs do here? Type <script>alert("asdf") and it alerts that immediately. .html() opens up that vulnerability, while .text() does not. If you're not expecting HTML, don't use it
@Ian agree, this is begging for second-order XSS.. I must punish with a downvote
Wow so instead of clicking "edit" and changing "val" to "text" I get 3 downvotes? Spaz much, it's the client-side. The user would be hacking his own client, way to think that one through. Also, way to give away that you have multiple accounts.
I was talking to Esailija :) Anyway my point stands. Putting .val() in the table isn't an XSS threat at all and would only inconvenience the user entering the HTML.
|
1

You can use jQuery's .change()

<input type="text" id="txt_name"  />

<script type="text/javascript">
$(document).ready(function(){
    $("#txt_name").change(function(){
        var blah = $(this).val();
        $('#id_of_cell').val(blah);
    });
})
</script>

2 Comments

That won't reflect new text until the field is blurred.
@AlienWebguy that's right. Your answer is the correct one. Will leave the erroneous answer here anyway.
0
$('#input1').keyup(function() {
 $('#input2').val($(this).val());
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.