JQuery - Cannot seem to bind event to dynamic DOM elements - javascript

Using JQuery UI Draggable, I am cloning elements as they leave an unordered list. As these are new to the DOM, i'm trying to use the JQuery On() method to bind an event which will show a hidden link. The anchor with the class cancel has display: none; in the css.
HTML
<ul class="current-campaign">
<li class="draggable">One <a class="pull-right cancel" href="#">
<i style="color:red">Icon</i>
</a>
</li>
</ul>
<ul class="new-campaign sortable"></ul>
JQuery
$(".sortable").sortable();
$(".draggable").draggable({
connectToSortable: ".sortable",
helper: "clone",
});
$(".current-campaign").on("mouseout", ".cancel", function () {
$(".cancel").show();
});
Really having trouble figuring out why the link doesn't show up when it leaves the unordered list. Here's a JS fiddle to see it in action.
http://jsfiddle.net/og937wy7/
FINAL EDIT WITH ANSWER
Armed with the knowledge of how to use the on() function, I fixed up my code so it's working as I intended.
$(document).on("mouseover", ".new-campaign", function (e) {
console.error($(this));
$(".new-campaign").find('.cancel').show();
});
http://jsfiddle.net/og937wy7/4/

You are attaching an event to .cancel, which is not at all in the view:
$(".current-campaign").on("mouseout", ".cancel", function () {
$(".cancel").show();
});
How can you mouseout, when .cancel doesn't have an area? Replace it with .draggable:
$(".current-campaign").on("mouseout", ".draggable", function () {
$(".cancel").show();
});
I guess you are looking for the cancel to be shown once you hover and when you come away, it should hide. So change your code to:
$(".current-campaign, .new-campaign").on("mouseout", ".draggable", function () {
$(".cancel").hide();
}).on("mouseover", ".draggable", function () {
$(".cancel").show();
});
I would also tell this is not a right way, because, it affects all the .cancel. So you might need to use $(this).find():
$(".current-campaign, .new-campaign").on("mouseout", ".draggable", function () {
$(this).find(".cancel").hide();
}).on("mouseover", ".draggable", function () {
$(this).find(".cancel").show();
});
Fiddle: http://jsfiddle.net/dpojx7so/
But, everything can be done using CSS itself!!!
You just need to add this CSS, instead of the whole JS:
.sortable:hover .cancel {
display: inline;
}
Fiddle: http://jsfiddle.net/mx58stx3/

Related

When dropped open textarea | jQuery

I'm new with jQuery. All I want is to make sortable. When something is dropped, it hides and open textarea with save button. When the save button is clicked text from textarea writes to a paragraph.
I searched a lot and I found these fiddles:
First one
Second one
I want to 'merge' their functionality, but when I try it's not working. Here is my code:
$('#sortable').sortable().droppable({
drop: function(ev, ui){
$(ui.draggable).html('<a id="send-thoughts" href="#">Click</a><textarea name="message"></textarea>');
}
});
$('#draggables li').draggable({
connectToSortable: '#sortable',
helper: 'clone',
revert: 'invalid',
cursor: 'move'
});
$('#send-thoughts').click(function()
{ var thought = $('textarea[name=message]').val();
alert(thought);
});
Here is a fiddle with my work - jsfiddle.net/CxpMn/102/ (sorry, I need more reputation to post more links). Please, help!
Several issues:
First, IDs cannot be duplicated in a page, so you need to use a class instead for #send-thoughts.
Second, you can't assign a click handler to elements that don't exist yet; so we need to delegate that event handler to an element that does exist, and target the future elements within it.
Third, we need to target the textarea that is related to the element clicked
$('#sortable').sortable().droppable({
drop: function(ev, ui){
// use class instead of ID
$(ui.draggable).html('<a class="send-thoughts" href="#">Click</a><textarea name="message"></textarea>');
}
});
// delegate event to account for future elements
$(document).on('click','.send-thoughts',function() {
// get the nearest textarea
var thought = $(this).siblings('textarea[name=message]').val();
alert(thought);
});
DEMO
The problem is that you are attaching a click handler to #send-thoughts before it is ever added to the DOM. Try something like the following:
$('#sortable').sortable().droppable({
drop: function(ev, ui){
$('#my-container').show();
}
});
$('#draggables li').draggable({
connectToSortable: '#sortable',
helper: 'clone',
revert: 'invalid',
cursor: 'move'
});
$('#send-thoughts').click(function()
{ var thought = $('textarea[name=message]').val();
alert(thought);
});
In your HTML put:
<div id="my-container" style="display: none">
<a id="send-thoughts" href="">Click</a><textarea name="message"></textarea>
</div>

Jquery on hover and out

I am trying to hide a div whenever I will hover over it and show another one in the same place.. And when I take the mouse out of that.. the previous div will be shown and this div will be hidden...
$(document).ready(function(){
$('#hover_tutor').hover(
function () {
$('#hover_tutor').hide();
$('#hover_tutor_hidden').show();
},
function () {
$('#hover_tutor').show();
$('#hover_tutor_hidden').hide();
}
);
});
<div id="hover_tutor">Blah</div>
<div id="hover_tutor_hidden" style="display:none;">Bleh</div>
But on hovering the hover_tutor... something is happening.. It's jumping up and down.. I don't know what's wrong...
You need to use .mouseenter event for #hover_tutor div and .mouseleave for #hover_tutor_hidden div:
$('#hover_tutor').mouseenter(function () {
$(this).hide();
$('#hover_tutor_hidden').show();
});
$('#hover_tutor_hidden').mouseleave(function () {
$('#hover_tutor').show();
$(this).hide();
}
).mouseleave();//trigger mouseleave to hide second div in beginning
Working Demo
You can also use toggle method instent of hide/show on hover event
<div id="hover_tutor" style="display: block;">Blah</div>
<div id="hover_tutor_hidden" style="display: none;">Bleh</div>
$('#hover_tutor').hover(
function () {
$('#hover_tutor').toggle();
$('#hover_tutor_hidden').toggle();
});
Working demo
http://jsfiddle.net/ivyansh9897/8jgjvkqk/
try this,
$('#hover_tutor').hover(function () {
$(this).hide();
$('#hover_tutor_hidden').show();
});
$('#hover_tutor_hidden').hover(function () {
$('#hover_tutor').show();
$(this).hide();
}
));
If you do have the flexibility to modify your html little bit using class attribute there's a better way. Use .toggle() to alter the state of your element on both mouseover and mouseleave.
HTML :
<div id="hover_tutor" class="hello">Blah</div>
<div id="hover_tutor_hidden" class="hello" style="display:none;">Bleh</div>
jQuery :
$(".hello").on("mouseover mouseleave", function(){
$("#hover_tutor").toggle();
$("#hover_tutor_hidden").toggle();
});
jsFiddle

JQuery reversing SlideToggle in Wordpress

I have the following simple code:
jQuery( document ).ready(function( $ ) {
$('.menu ul li:has("ul")').hover(function() {
$('>ul', this).stop().slideToggle();
});
});
I'm using the 'no conflict' way of firing jQuery but for some reason my slide toggle is closing on hover rather than opening! What am I doing wrong? I'm sure its simple but I just can't see it.
An example can be seen here http://ng1club.everythingcreative.co.uk
Use mouseenter and mouseleave instead. Sometimes hover without the second function as a hover out, triggers twice.
$('.menu ul li:has("ul")').mouseenter(function() {
$('>ul', this).stop().slideDown();
}).mouseleave(function() {
$('>ul', this).stop().slideUp();
});
Try this according to documentation you can use hover and apply effect on hover and hover out
jQuery('.menu ul li:has("ul")').hover(
function() {
jQuery('>ul', this).stop().slideDown();
}, function() {
jQuery('>ul', this).stop().slideUp();
}
);
.hover()
Sample Fiddle

Display hidden element with the help of jquery

I have some code in a jsfiddle and I want to display one hidden element <div id="e1"></div>
I have written code for this in jquery like this:
$("#credit4").sortable({
receive: function (event, ui) {
ui.item.remove();
var s="PLEASE SELECT ANOTHER BLOCK";
$("#e1").show();
$("#e1").html(s);
setTimeout('$("#e1").hide()',1500);
}
});
credit4 is the id of a draggable element and when the user wants to drag the element then this hidden elemet should be displayed.
You can also check my jsfiddle here - http://jsfiddle.net/sanjayrathod7/5cZD5/44/.
Please suggest me where I'm wrong.
Here's a simplified fiddle with a set of alerts in place indicating which line is hiding #e1. You can see that you're probably hiding it when you didn't intend to (at lines 45 and 171):
http://jsfiddle.net/isherwood/5cZD5/50
$("#credit").sortable({
receive: function (event, ui) {
ui.item.remove();
$("#e1").show();
$("#e1").html(s);
setTimeout('$("#e1").hide()', 1500); alert('hiding 10');
}
});
I have find out the answer
Try this
$( "#credit4" ).draggable({
revert: true,
start: function( event, ui ) {
var s="PLEASE SELECT ANOTHER BLOCK";
$("#e1").show();
$("#e1").html(s);
setTimeout('$("#e1").hide()',1500);
}
});

Show/Hide after a div created dynamically

I use this code to show/display Edit link when mouse hovers over the start div. This div however can be created dynamically and when it's created the code below doesn't work.
$(".start").hover(
function() {
timeclock.utils.displayEdit(this)
},
function() {
timeclock.utils.hideEdit(this)
});
I tried the code below but it doesn't work and it looks wrong. How can I implement $(document).on('hover'.....) to hide/show the Edit link as shown above?
$(document).on("hover", ".start",
function() {
timeclock.utils.displayEdit(this)
},
function() {
timeclock.utils.hideEdit(this)
});
hover() is a shortcut for binding mouseenter and mouseout handlers. Your second example doesn't work because on() doesn't take two functions like that. You bind multiple handlers at once using delegated events like this:
$(document).on({
mouseenter: function () {
timeclock.utils.displayEdit(this);
},
mouseleave: function () {
timeclock.utils.hideEdit(this);
}
}, '.start');
Simple example: http://jsfiddle.net/TRcR9/
There are 2 errors in your code.
1. you should use $(this) instead of this. There is a different between this two.
2. you have to bind the hover again whenever a new div is created.
Your syntax is a little off. You can attach multiple event handlers simultaneously using a plain object.
$(document).on({
mouseenter: function(){
timeclock.utils.displayEdit(this);
},
mouseleave: function(){
timeclock.utils.hideEdit(this);
}
}, ".start");
I've created a Codepen example here: http://cdpn.io/dDewi
The syntax you have is a a little off. Here is a jsfiddle with a working example:
HTML:
<div id="container"></div>
CSS:
#edit { display: none; }
Javascript:
$(function() {
$(document).on(
{
mouseenter: function()
{
$('#edit').show();
},
mouseleave: function()
{
$('#edit').hide();
}
},
'.start'
);
$('#container').prepend('<div class="start">Mouse over me <a id="edit" href="#">edit</a></div>');
});

Categories

Resources