Coltech
Coltech

Reputation: 1710

Get textbox HTML and value with Jquery

I have a text box in a div:

<div id="divTest">
    <input type="text" id="txtText">
</div>

Suppose the user types a word into the text box, I want a jquery method of getting the textbox HTML with the value that the user typed in. I have tried the below code:

$('#btnTest').click(function(){
    alert($('#divTest').html());
    e.preventDefault();
});​

But this only gives me the HTML of the textbox, not the HTML + the value of the text within the textbox.

Here is a jsFiddle of the problem.

Upvotes: 0

Views: 3960

Answers (2)

Stefan
Stefan

Reputation: 3900

var html_input = $("#divTest").html();
var val = $("#txtText").val();
var input = html_input.slice(0,-1) + " value='" + val + "'>";
alert(input);

Upvotes: 1

Machinegon
Machinegon

Reputation: 1885

var html = $('#divTest').html();
var value = $('#divTest').val();
var message = html + value;

Upvotes: 0

Related Questions