Textarea not working with jQuery live keyup function

I am trying to create a tag friends system for a personal project. I found some great tutorials online, but right now I'm stuck.

Nothing happens when I type @ in tagbox. The display and msgbox must slide down so that users can select the friend they want to tag.

What am I doing wrong?

<div id="tagbox"> <div class="textarea"> <textarea id="contentbox" name="contentbox"></textarea> <div id='display'></div> <div id="msgbox"></div> <button type="submit" id="tag_button" value="Update" name="submit">Add</button> </div> 

And here is the AJAX / jQuery part:

 <script type="text/javascript"> $(document).ready(function() { var start=/@/ig; var word=/@(\w+)/ig; $('#tagbox .textarea textarea[name="contentbox"]').live("keyup",function() { var content=$(this).text(); var go= content.match(start); var name= content.match(word); var dataString = 'searchword='+ name; if(go.length>0) { $("#msgbox").slideDown('show'); $("#display").slideUp('show'); $("#msgbox").html("Type the name of someone or something..."); if(name.length>0) { $.ajax({ type: "POST", url: "addfriends.php", data: dataString, cache: false, success: function(html) { $("#msgbox").hide(); $("#display").html(html).show(); } }); } } return false(); }); $(".addname").live("click",function() { var username=$(this).attr('title'); var old=$('#tagbox .textarea textarea[name="contentbox"]').html(); var content=old.replace(word,""); $('#ws3 .textarea textarea[name="contentbox"]').html(content); var E="<a class='red' contenteditable='false' href='#' >"+username+"</a>"; $('#tagbox').append(E); $("#display").hide(); $("#msgbox").hide(); $('#ws3').focus(); }); }); </script> 
+4
source share
1 answer

To get the value of textarea , you must use the .val() method instead of text() otherwise the content will always be empty.

 var content=$(this).val(); 
+3
source

Source: https://habr.com/ru/post/1485334/


All Articles