benhowdle89
benhowdle89

Reputation: 37464

How to save text to the DOM

How can i achieve this using jQuery. I have two text inputs on the page, so when the users has entered text and left the input field, for that text to be inserted into the DOM so i can access it?

EDIT:

To clarify i have this:

Expected Delivery Date: <input id="exp" type="text"></input>

What i'm trying to do is print the contents of the page, but its not printing this input value?

This is the only input i need from the user so i wanted to avoid having to submit the form just for one input!

Upvotes: 0

Views: 1465

Answers (2)

David Hedlund
David Hedlund

Reputation: 129792

Something like this?

$('.my-input-fields').blur(function() {

   var div = $('<div/>');
   div.html( $(this).val() );

   $('#DOMplaceOfEntry').append(div);

});

Upvotes: 1

Nick Craver
Nick Craver

Reputation: 630389

Your inputs are already in the DOM, you can get their value using .val() like this:

var myValue = $("#inputID").val();

You may need to use a different selector but the idea stays the same, it's $(selector).val(), or possible this, for example if you want the value when the user leaves:

$("input").blur(function() {
  alert($(this).val());
});

You can test it here.

Upvotes: 2

Related Questions