Smile
Smile

Reputation: 2758

How to set jQuery Events only on Textarea?

I would like to know that How to set jQuery Events only on Textarea and not other elements. Please look at the following code:

<div id="divUnique">
    <textarea id="txtArea" name="txtArea" cols="100"></textarea>
    <input type="file" name="myfile" id="myfile" size="20"/>
    <input type="checkbox" id="chk" name="chk"/>
</div>

I will have a common DIV called divUnique and, later, dynamically pairs of TEXTAREA, FILE INPUT and CHECKBOX will be added.

I want to set three events called click, change and keypress only on TEXTAREA and not other elements in DIV.

So, How can I achieve this?

Upvotes: 1

Views: 104

Answers (2)

Ronjon
Ronjon

Reputation: 1849

Try this

$(document).ready(function(){
    //click
    $("#txt").click(function(){
        alert("textarea clicked");
    });
    //keypress
    $("#txt").keypress(function(){
        alert("key pressed");
    });
    //change
    $("#txt").change(function(){
        alert("changed");
    });
})

demo link: Jsfiddle

Upvotes: 1

Rituraj ratan
Rituraj ratan

Reputation: 10378

$("#divUnique").on("click change keypress","#txt",function(){
    // your work goes here
});

reference on

Upvotes: 3

Related Questions