简体   繁体   中英

How do I a use Javascript or ajax to inject or add a the value of an input field in a textfield in real time?

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'"

 // 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM