This question already has answers here:
Event binding on dynamically created elements?
(23 answers)
Event handler not working on dynamic content [duplicate]
(2 answers)
Closed 9 years ago.
My problem is, that even when I use .on, the click event over added div with class added does not triggers.
This is my script from page.
$(".added").on("click", function () { alert("halo") });
$("#nextAdress").on("click",function (event) {
event.preventDefault();
$("#trainerRegistrationFields").append('<div class="added">adsa</div>');
}
Can you please help me so the click event will trigger also on the dynamically added element with class added?
Tahnk you in advance
You can use event delegation syntax of on for dynamically added element.
$("#trainerRegistrationFields").on("click",".added", function (){
alert("halo") ;
});
In your code you are binding the handler only to the element that existed in DOM during the time of event binding. You can also do this way:
or add the handler here while you append the item.
function handleClick(){
alert("halo");
}
$(".added").on("click", handleClick); //for the already existing elements
$("#nextAdress").on("click",function (event) {
event.preventDefault();
$("#trainerRegistrationFields").append($('<div class="added">adsa</div>').on('click', handleClick));
}
Related
I want to capture the tag added and removed event on select of a tag field in Coral UI 3. I have added this in a clientlib which has category of cq.authoring.dialog.all.
(function($, $document) {
$document.on("dialog-ready", function() {
$('[data-fieldname="./cq:tags"]').on('itemadded', function(ev, value) {
console.log("Tag added");
});
});
})($, $(document));
The sling:resourceType of tag field is Coral UI 3 specific: cq/gui/components/coral/common/form/tagfield
But this event is no captured. If I change tag field resourcetype to old Touch UI specific sling:resourceType as cq/gui/components/common/tagspicker then it works.
How do I capture event in Coral UI 3 tag field?
If you look at /libs/cq/gui/components/coral/common/form/tagfield/render.jsp (at least in AEM 6.4), you'll see it renders the following HTML structure:
<foundation-autocomplete>
<coral-overlay></coral-overlay>
<coral-taglist>
...
</coral-taglist>
</foundation-autocomplete>
foundation-autocomplete's documentation can be found here: https://helpx.adobe.com/experience-manager/6-4/sites/developing/using/reference-materials/granite-ui/api/jcr_root/libs/granite/ui/components/coral/foundation/clientlibs/foundation/js/autocomplete/index.html
But what you are interested in is the coral-taglist the documentation for that is here: https://helpx.adobe.com/experience-manager/6-4/sites/developing/using/reference-materials/coral-ui/coralui3/Coral.TagList.html
the items in a coral-taglist are of type Coral.Collection whose docs are here: https://helpx.adobe.com/experience-manager/6-4/sites/developing/using/reference-materials/coral-ui/coralui3/Coral.Collection.html#Coral.Collection:events
so you can do something like:
document.querySelector(<coral-taglist element selector>).on('coral-collection:add', function () {
// handle add
})
document.querySelector(<coral-taglist element selector>).on('coral-collection:remove', function () {
// handle remove
})
I have an XML view that contains a TileContainer which is bound to a model that is used to create StandardTiles. The XML snippet is:
<TileContainer id="tilelist" tiles="{Applications}">
<tiles>
<StandardTile name="{ID}" icon="{Icon}" title="{Name}" press="doNavigation" info="{Description}"
number="{path : 'Number', formatter: 'linxas.com.fiori.launchpad.util.Formatter.formatUsingURL'}"
numberUnit="{NumberUnit}"/>
</tiles>
</TileContainer>
This is working perfectly, the correct tiles are getting displayed etc. When I click on a tile, there is navigation that occurs and I want to "remember" which tile was clicked (by index) so when returning I can scroll to that tile. This is done on the tile's press event handler (doNavigation function) and stores the index in sessionStorage. This is also working properly.
doNavigation : function (evt) {
if (sessionStorage && this.getView().byId('tilelist')) {
sessionStorage.setItem("selected_tile", this.getView().byId('tilelist').indexOfTile(evt.getSource()));
}
...
}
The proper value is stored. So when navigating back, within the onAfterRendering function of the page that contains the TileContainer I have the following code. It is attempting to see if there is a "selected_tile" value stored in sessionStorage, if so it calls scollIntoView passing in the tile index. The issue is that this code is executed, but doesn't work and I suspect it is because at the time of calling this function, the TileContainer's tiles aggregation is returning 0 length.
onAfterRendering : function (evt) {
var theList = this.getView().byId("tilelist");
if (sessionStorage && theList) {
var tile_index = sessionStorage.getItem("selected_tile");
console.log(tile_index + " of " + theList.getTiles().length);
if (tile_index) {
theList.scrollIntoView(+tile_index, true);
sessionStorage.removeItem("selected_tile");
}
}
}
My console output looks something like this (based on the tile that was clicked):
5 of 0
Any help would be appreciated. I assume that there is somewhere else that I need to execute this last bit of code as the TileContainer does not seem to be finished processing its tiles at this point, at least that is my assumption of why the tiles aggregation is 0.
Are you using Routing in your project?
If yes, you can try to register a method to handle the routePatternMatched event of the router. This method will be called after the onAfterRendering method - if the proper route pattern is matched.
To achieve this, just create the following:
onInit: function() {
sap.ui.core.UIComponent.getRouterFor(this).getRoute("NameOfYourCurrentRoute").attachPatternMatched(this._routePatternMatched, this);
},
_routePatternMatched: function(oEvent) {
//do your stuff here
},
Hopefully the TileList is ready at this point to navigate to the correct tile.
Here's a simple fiddle to demo my situation...
http://jsfiddle.net/UnsungHero97/EM6mR/17/
What I'm doing is adding an event handler for current & future elements, using .on(). I want to be able to remove these event handlers for specific elements when something happens; in the case of the fiddle, when the radio button is selected, the event handler for the blue elements should be removed and clicking those elements should not do anything anymore.
It doesn't seem to be working :(
How do I remove the event handler attached to document that I created with .on() for those specific blue elements?
The signature for your .on() and .off() has to match.
These two do not match so the .off() call won't find matching event handlers to remove:
$(document).on('click', '.btn', function() {
update();
});
$(document).off('click', '.blue');
Note, the selector passed to .on() and .off() is different.
When using the dynamic form of .on() (where you pass a selector as an argument to .on()), you can't remove just part of the items. That's because there's only one event handler installed on the root element and jQuery can only remove the entire thing or not at all. So, you can't just .off() some of the dynamic items.
Your options are to remove all the event handlers with:
$(document).off('click', '.btn');
and, then install a new event handler that excludes the items you don't want such as:
$(document).off('click', '.btn:not(.blue)');
Or, teach the event handler itself how to ignore .blue items:
$(document).on('click', '.btn', function() {
if (!$(this).hasClass('blue')) {
update();
}
});
Be careful of how you attach your events; this works fine for me:
$('.btn').on('click', function() {
update();
});
$('#disable').on('change', function() {
$('.btn').off('click');
});
Only way seems to be:
$('#disable').on('change', function() {
$(document)
.off('click', '.btn')
.on('click', '.btn:not(.blue)', update);
});
In this post the question is asked how to initiate an immediate drag operation when another object is clicked. I have the same question but in the context of gwtquery and the draggable plugin.
In the reference post the new item to be dragged is moved to be under the click point and the original mouse down event is sent on to it. Is it possible to do this in gwt?
You should be able to do something like that :
$("#myDraggable").as(Draggable).draggable();
$("#myOtherDiv").mousedown(new Function(){
#Override
public boolean f(Event e) {
//position your draggable first
//...
$("#myDraggable").trigger(e);
return false;
}
});
But the method trigger(Event e) doesn't exist yet in GQuery. I open an issue for covering that : http://code.google.com/p/gwtquery/issues/detail?id=139
I understand that this is a probably a noob-ish question, but I've had no luck with the other threads I've found on the same topic.
I've devised a workaround to hack a views exposed filter to hide and show products with a stock count of "0". The exposed filter for the stock count (input#edit-stock) is hidden with CSS and inside a custom block is a link to manipulate the form and trigger the query (with ajax). This is working great, but with one exception - after resetting the form with the views-provided "reset" button, toggle() will not rebind properly to the link, and click won't fire the first time. Works fine on the 2nd click. I'm sure that the solution is very simple, but I'm at a loss..
How to rebind toggle() effectively?
Sorry, I'm unable to provide a live example. Many thanks for any input.
CUSTOM BLOCK:
<a id="toggle" href="#">exclude</a>
JQUERY:
$(document).ready(function () {
var include = function () {
$('input#edit-stock').attr('value', 0).submit();
$('a#toggle').html('include');
};
var exclude = function () {
$('input#edit-stock').attr('value', '').submit();
$('a#toggle').html('exclude');
};
$('a#toggle').toggle(include, exclude);
$('input#edit-reset').live('click', function (event) {
$('a#toggle').unbind('toggle').toggle(include, exclude).html('exclude');
});
});
if i get the problem right you need to reset the toggle. Why instead of unbind toggle and rebinding it you just don't simulate a click if the link is == to include?
$(document).ready(function () {
var include = function () {
$('input#edit-stock').attr('value', 0).submit();
$('a#toggle').html('include');
};
var exclude = function () {
$('input#edit-stock').attr('value', '').submit();
$('a#toggle').html('exclude');
};
$('a#toggle').toggle(include, exclude);
$('input#edit-reset').live('click', function (event) {
//if the link is include, click it so that it resets to exclude, else do nothing
if ($('a#toggle').html() == 'include'){
$('a#toggle').click();
}
});
});
fiddle here: http://jsfiddle.net/PSLBb/
(Hope this is what you were looking for)