3

I am trying to display the value of #response from the code below into the input field. I am not sure how that can be done. If I use

<div id="response"></div> 

that will display the result but I like to display it in the input field. I tried this,

<input type="text" id="response"/>.

This did not display the result Any idea how that could be done. Thanks in advance.

<script>
        $(document).ready(function() {

            $("#productID").change(function() {
                //alert($('#productID option:selected').val());
                var pId = $('#productID').val();

                $.get('updateProduct', {
                    productID: pId.trim()    //using trim to get rid of any extra invisible character from pId                    
                },
                function(responseText) {
                  $('#response').text(responseText);                       

                });

            });
        });
    </script>

$('#response').val(responseText); --> If this contains two values then is there a way to retrieve those values separately? what I would like to do is display values separately into two separate input field.

 $('#response').val(responseText);

This could contain values such as, Product Description, Product comments, Product type. Those values are coming from the servlet. After receiving those values they will be displayed into three separate input field.

At the moment if I do the following,

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

This will display all three values in the same input field.

2 Answers 2

2

input element values are set using jQuery's val() method, not text().

Change:

$('#response').text(responseText);  

To:

$('#response').val(responseText);  

JSFiddle demo.

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

6 Comments

Not sure should I ask separate question or not. Is there any way to loop through this value, $('#response').val(responseText);
@ron you should ask a separate question. Based on what you've provided here I'm not fully sure what you mean.
I will ask separate question. Before I do it I will try to explain it to you. $('#response').val(responseText); --> If this contains two values then is there a way to retrieve those values separately? what I would like to do is display values separately into two separate input fields. Hope it make sense...
@ron you'd have to let me know what responseText contains. You'll have to put that in your question.
I have edited my question. Could you please have a look. Thanks
|
1

Use .val instead of .text

$("#response").val(responseText);

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.