Reputation: 4656
I searched a lot through questions but I did not find the right way. My problem is: I want to create a div
dynamically , showing it on mouseover
appending it to another div
, and remove (through remove()
function) on mouseout
. I tried couple ways but in any of them , sometimes the div
shows up and then disappear, sometimes it doesnt, sometimes it disappear when my mouse goes away from the text in the container div
.
Thank you guys .
var usr = 'username_pre';
var newdiv = $('<div>', {
html: '<a href="#" title="">'+usr+'</a> </br> <a href="#" title="">impostazioni</a> </br> <a href="#" title="">esci</a>'
});
$("#container").mouseover(function(){
$("#options").css('visibility','visible').append(newdiv);
});
$("#options").mouseout(function(){
$(newdiv).remove();
});
Upvotes: 4
Views: 2215
Reputation: 4656
I solved my problem using :
This code html :
<div id="container" class="cont">
<a href="#" id="username" class="nomeutente" title=""> Stefano Imparato </a>
</div>
<div id="options" >
<a href="#" id="user" title="">usr</a> </br>
<a href="#" title="">impostazioni</a> </br>
<a href="#" title="">esci</a>
</div>
Code jquery :
$("#container").mouseover(function(){
$("#container").append($("#options"));
$("#options").css({
'display' : 'block'
});
$(this).find('#user').text(usr);
});
$("#container").mouseout(function(){
$("#options").css('display','none');
});
css :
#option { display: none; }
Upvotes: 1
Reputation: 4656
This is html :
<div id="container" class="cont">
<a href="#" id="username" class="nomeutente" title=""> Stefano Imparato </a>
<div id="options" class="opt"></div>
</div>
Upvotes: 0
Reputation: 12683
Does this help:
$("#container").mouseover(function(){
$("#options").css('display','block').append(newdiv);
});
$("#options").mouseout(function(){
$("#options").css('display','none');
$(newdiv).remove();
});
Upvotes: 1