The Pied Pipes
The Pied Pipes

Reputation: 1435

jQuery text() function not accepting input.val()

I'd like to set up a preview area that shows what some inputted text looks like in situ. However, I'm struggling to get this jQuery function with text() to work.

whilst this works (displays "some sample text" in #preview when the form is submitted:

$("form").submit(function () {
              $("#preview").text("some sample text").show();
              return false;
            });

this doesn't work, and shows nothing when I try to take the contents of the text field and use that instead in the preview box:

$("form").submit(function () {
              $("#preview").text($("input:first").val()).show();
              return false;
            });

Any idea why? Probably dead simple and I'm just missing it.

Upvotes: 1

Views: 88

Answers (2)

The Pied Pipes
The Pied Pipes

Reputation: 1435

In the end, I rewrote this as more of a "live preview", and used the keyup() function to capture the input's val instead of requesting after the form submit.

Upvotes: 0

pete
pete

Reputation: 25081

The code works fine in a fiddle. Make sure #preview has a declared height and width, otherwise it may not be visually distinct.

See working demo: http://jsfiddle.net/js4XU/

Upvotes: 1

Related Questions