jQuery concatenate string to input field

Jadin Stojadin

I'm trying to concatenate string and display it into my textbox when a td in my table has been clicked.

I did this so far:

   $(document).on("click", ".keywordClick", function () {
            $('.txtKeywords').val($(this).attr('value'));
        });

.keywordClick is td item which contains required string 

And

.txtKeywords is the textbox which is empty initially...

Each time someone clicks on string I'd like it to be like this:

"Hello world today" => initial click;

"is a sunny day" => second click;

So the output would be like:

Hello world today is a sunny day... Between each concatenated string should be a empty space " " << like this... Can someone help me out?

Milind Anantwar

You can use callback function of .val() which has a function returning the value to set. It receives the index position of the element in the set and the old value as arguments. You can use old value to be concatenated with new value:

$(document).on("click", ".keywordClick", function () {
 var appendText = " " + $(this).attr('value');
  $('.txtKeywords').val( function( index, oldVal ) {
     return oldVal + appendText;
  });
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

HotTag

Archive