user628298
user628298

Reputation: 287

Data only to click the div

Good morning, everyone.

I have a doubt. I have a list of div. textarea with text inside it and inside. text for a button event. Want to get only the data from the textarea div that the person clicking the button.

I would be very grateful for the help. thanks

Example code.

<div class="text">
 <textarea id="texts" class="texts" rows="5" cols="45" name="textarea"> </ textarea>
 <div class="bt" id="1234556">
  <div class="button"> Send </div>
 </div>
</div>

Upvotes: 0

Views: 81

Answers (3)

Mark Coleman
Mark Coleman

Reputation: 40863

Since you have a list of div.text your best bet is to query based upon the structure of your DOM.

$(".button").click(function() {
    var $textArea = $(this).parents(".text").find(".texts");
    //Do something with $textArea.Val();
});

What we simply do is call .parents() on the current div.button which will allow us to get the div.text element. From there you can simply find your textarea.texts element and get the corresponding value.

Code example on jsfiddle.

Upvotes: 0

Alnitak
Alnitak

Reputation: 339836

$('.button').click(function() {
    var text = $('#texts').val();

    // do something with text
    alert(text);
});

Working demo at http://jsfiddle.net/PPcxm/

Upvotes: 2

Alex
Alex

Reputation: 7374

As long as you have the same structure (and presuming you have more than one example on the page as the button is a class and textarea is an id, the following would work:

$(".button").click(function()
{

   var text = $(this).parent().prev().val();

});

Upvotes: 1

Related Questions