I have a dropdown
that I want to position in the last character of the textarea
, but I don't know how to achieve it, I have investigated how to capture the cursor position of the textarea
but I have not found something useful and I have no idea how to do it
This is my code I have so far:
$(function(){
$('textarea').on('keyup',function(evt){
if(evt.which >= 96 && evt.which < 106){
//console.log(String.fromCharCode(evt.which-48))
}
//console.log($('textarea').val());
if($('div.dropdown').hasClass('open')){
$('div.dropdown').removeClass('open');
}else{
$('div.dropdown').addClass('open');
}
})
});
textarea{
border: 1px solid gray;
height: 100px;
min-width: 100%;
max-width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<textarea></textarea>
<div class="dropdown open">
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</div>
textareaHelper is a jQuery plugin that could solve your problem
Here an example of how it works:
Adapted from this SO answer which does what I'm really looking for and gives me the result I'm really looking for.
What is done is to create a copy of the
styles
deltextarea
indiv
which the cursor of the would bediv
aspan
Original code on GitHub
My code would be as follows: