3

I have a text_field of id #image_tag_list_tokens and the textfield appears as follows in the image form:

 = f.text_area :tag_list_tokens, label: "Tags (optional) ->", data: {load: @image_tags }, label: "Tags"

I have an input field and a button as follows:

 <input type="text"  name="myNewTag" id="my_new_tag">
 <button name="meNewTagButton" type="button" id="createMy_new_tag">Create new tag</button>

When the user types in the input field a new tag, I want to grab that new tag and add it inside the text area of :tag_list_tokens without reloading the page. The value of the tag_list_tokens textfield is of the following format :

 "old_tag1,old_tag2,'new_tag1','new_tag2'"
codigomonstruo
  • 1,081
  • 1
  • 11
  • 45
  • Just FYI - it's usually more helpful if you post your rendered HTML instead of your rails .erb template :) – larz Aug 19 '16 at 02:44

1 Answers1

1

// add event listener for button click
$("#createMy_new_tag").on("click", function(){
  // get the text in the input
  var new_tag = $("#my_new_tag").val(),
  // grab the textarea
      textarea = $("#image_tag_list_tokens")
  // append the input to the current text
  textarea.val(textarea.val() + ", " + new_tag)
  // reset the input
  $("#my_new_tag").val("")
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="image_tag_list_tokens">blah</textarea>
<input type="text" name="myNewTag" id="my_new_tag">
 <button name="meNewTagButton" type="button" id="createMy_new_tag">Create new tag</button>
larz
  • 5,724
  • 2
  • 11
  • 20