Sylarman5
Sylarman5

Reputation: 110

Select contents of Textare when you have it more

In my html document, i have 5 or more textarea like this:

<textarea name="comente5422" id="comente5422" placeholder="Tap your coment..."></textarea>

As you see the textarea have ID: comente5422 and for others the number change like this:

comente5423, comente5424, ...

And this is the JS script to get the contents on tap the enter button

<script type="text/javascript">$('#comente').keypress(function(event) {

    if (event.keyCode == 13 || event.which == 13) {

var blae = $('#comente').val();
var is_ownn = $('#son_owni').text();

  $.ajax({                       
                type: 'post',
                url: 'call.php', // point to server-side PHP script 
                data: {
            coment: blae<?php echo $publication['id_share'];;?>,
            is_own: is_ownn<?php echo $publication['id_share'];;?>,
        },  
                success: function(scrip_response){

                }
     });
    }
});</script>

this script work when i duplicat it in each textarea when use the ID of each textarea and the html code become very busy.

I want to know if there is an other method to get the contents of the textarea without duplicate this script!

Upvotes: 1

Views: 46

Answers (1)

brk
brk

Reputation: 50346

You can use name selector and this.If name is different , then use a common class

$( "textarea[name='comente5422']" ).keypress(function(event) {
 if (event.keyCode == 13 || event.which == 13) {
 var blae = $(this).val();
 var is_ownn = $('#son_owni').text();
 //... rest of code

Or can also use attribute-starts-with-selector

$( "textarea[id^='comente']" ).keypress(function(event) {
     if (event.keyCode == 13 || event.which == 13) {
     var blae = $(this).val();
     var is_ownn = $('#son_owni').text();
     //... rest of code

Upvotes: 1

Related Questions