Hades Black
Hades Black

Reputation: 11

Changing content with .change Jquery event

Well as the title says, I want to change (or add) text in "p" html tag, after I write something inside "input".I thought it would be simple, but somehow I can't get it to work. This is an example code:

test.php

<form>
<input id="text" class="text" />
</form>
<p id="output"></p>

test.js

$("form").change(function()
{
    document.getElementById("output").innerHTML = "asd";
});

I can't find the the error, although when I use:$(document).change... instead it works fine... I guess the problem is simple but I just keep missing it.

Upvotes: 1

Views: 50

Answers (3)

lante
lante

Reputation: 7336

You must check for changes into the input element. Use the following code:

$("input").change(function() { 
    // your action
});

Upvotes: 2

Mohamed-Yousef
Mohamed-Yousef

Reputation: 24001

DEMO

 $(".text").on('keyup',function()
    {
    $("#output").text( $(this).val());
   });

Upvotes: 1

Maximiliano Hornes
Maximiliano Hornes

Reputation: 101

How are you?

If you want to listen the input change event, you have to subscribe the event to that input.

$("#text").change(function() {});

Or:

$("body").change("#text", function() {});

Also, if you want to listen while the user types, you can use:

$("#text").on("change keyup", function() {});

Or:

$("body").on("change keyup", "#text", function() {});

I hope that helps you.

Regards.

Upvotes: 0

Related Questions