Jquery binds the click events sequentially after every .load() - events

I am loading a .php file into a div tag using .load() function of jquery :
$('#content').load('MY_URL');
I have bound several events using .live and all of them work fine after loading the MY_URL.
but the problem is : when I load the second page into the latter loaded page (suppose #content) the events are fired twice :
for example I have bound a click event to show and hide a div tag but after loading the second page it fires two times and shows and hide the div tag sequentially that you can say it doesn't work at all.
I have read all problems like this and I can say all their problems have been solved using .live instead of .bind but mine did not get solved ! please help me.

Unbind your first live event before firing 2nd live event
$('#content').die("click");

Related

Text in CKEDITOR field not being submitted in the first submission

The text in ckeditor field is not sent when submitting forms for the first time (only on the second time, third time, etc).
For example, If try to create an article's post and submit the form I'll get a validation error: 'The field body is required'. If try to submit again (for the second time or third time), It will work well.
The real problem is when editing! For example, when editing a form the field 'body', among others fields, is filled out with the data from the database. In other words, there are already text in the ckeditor field.
If I try to submit the form for the first time it will not update the body because the text in the ckeditor is not sent; what is sent is the default value (the old article's body, which was filled out with data from the DB).
Therefore, it won't edit unless I get a validation error in other field (if I get a validation error, I'll have to submit again, and that will work).
How to solve this problem? Is this an known bug in CKEDITOR 4? If I don't solve it the users will feel frustrated if they have to submit the form at least twice to edit or to create an article.
Here is a list of plugins I'm using (may be useful to solve the problem):
a11yhelp, about, api, autocomplete, autocorrect, browser, clipboard, colordialog, copyformatting, crossereference, dialog, div, docprops, find, googlesearch, image, link, liststyle, magicline, mathjax, openlink, pastecode, pastefromword, preview, quicktable, scayt, section, showblocks, sourcedialog, specialchar, table, tableselection, tabletools, tabletoolstoolbar, texttransform, widget, wsc
By the way, I downloaded ckeditor using ckeditor builder in their official website.
I opened this issue in GitHub and a guy figured out the problem. His proposed solution worked wel!! Here is what he said:
Workaround
As the issue is more tricky to fix than it seems, for now I propose a
simple workaround: invoke ajaxRequest not on $( document ).ready,
but rather on editor's loaded event:
CKEDITOR.replace( 'editor', {
on: {
loaded: function() {ajaxRequest();}
}
});
Explanation of the issue
The issue is connected with how DOM listeners are registered for given
element:
The order of event listeners for a particular event type will always
be:
The event listeners registered with addEventListener() before the first time the event handler's value was set to non-null
Then the callback to which it is currently set, if any
Finally, the event listeners registered with addEventListener() after the first time the event handler's value was set to non-null.
In case of CKEditor 4, the value of the form's element is modified by
editor._attachToForm private method, which adds event listener to
form's submit event:
ckeditor-dev/core/editor.js form.on( 'submit', onSubmit );
However this listener is added on loaded event, which is fired
asynchronously when editor is loaded – so after registering
synchronous onsubmit handler with the validation logic. This way
editor's field is updated after validating.
Proposed solutions
Update editor's element on formdata event. This way we would have
total control over data being submitted and we would be sure that
correct data is set before submit event. The problem with this
solution is the fact that browsers' support is non-existent; the event
will appear in Chrome 77, however it is still not known if and when
the support will appear in Firefox or Safari.
Update editor's element on every change in the editor's content thanks
to change event. This solution will also fix cases, where some other
scripts are using value not from the editor, but directly from the
replaced textarea – they would get fresh data more often then only
after submitting the form. However this solution requires #1364, which
connects with a pretty big refactoring.
NOTE: AjaxRequest is the function I was using to submit the form togehter with Jquery.

Reinitialize bootstrap modals after loading ajax content

I'm using Bootstrap Modals, with normal usage cases it works well, but when adding new content via AJAX I need to make it remap the newly added elements.
I need that function like bootstrap_modal() or bootstrap_modal.init() to be fired after the new content was added.
I have a table of elements, each element has a modal that contains more information about it, these elements are added using AJAX (with a click of a button) so I want that when I click on that button, the new content loads and the the modal remapping is triggered to take into account the new elements.
I don't have enough rep. to leave a comment on your question, but can you paste some code and let me know what programming language you're using? I have examples in both c#/ASP.NET and php.

Jquery window scroll / resize works on bind, not on live

So I have an advanced search form, and when you scroll down the results, the search results summary sticks to the top of the page as you scroll using the 'sometimes fixed' code described here - http://www.bennadel.com/blog/1810-Creating-A-Sometimes-Fixed-Position-Element-With-jQuery.htm
This all works very nicely, but when some of the form elements are changed, it reloads the whole form, results summary, and results list using AJAX.
I noticed it was using bind to check for the scroll / resize, but the element it needs to effect has obviously been replaced with AJAX, so I changed it to live. This didnt work, on further digging I found out that initially live() only supported certain events, and then was made to work on all events, including custom ones, so why does it still not seem to be working with scroll?
In jQuery 1.3.x only the following JavaScript events (in addition to
custom events) could be bound with .live(): click, dblclick, keydown,
keypress, keyup, mousedown, mousemove, mouseout, mouseover, and
mouseup. As of jQuery 1.4 the .live() method supports custom events as
well as all JavaScript events that bubble. As of jQuery 1.4.1 even
focus and blur work with live (mapping to the more appropriate,
bubbling, events focusin and focusout). As of jQuery 1.4.1 the hover
event can be specified (mapping to mouseenter and mouseleave, which,
in turn, are mapped to mouseover and mouseout).
Have tried it with jQuery 1.4.1, 1.5.1, 1.6.2 and no joy. Am I doing something stupid, or is what I am trying to do just not possible?
Wasnt looking likely I would get an easy solution...
So I rewrote the way the search script worked so that the 'sometimes fixed' div wasnt regenerated by ajax, and it loaded the form, results summary, and results directly into their divs

Infinite scrolling inside a div with AJAX (jQuery) loaded date

I've been trying to figure this out over several questions here on SO, finding out the different parts and then assembling them, but now I'm giving up.
What I'm trying do do:
The user is on a page, clicks a button and a list of content loads into a div using jQuerys .ajax. When he or she scrolls to the bottom of the div, load more content. As seen on Twitter, but this is inside a div.
I've seen examples on how to have infinite loading in scroll, but the problem is that those examples reload the entire page (it seems)
Has anyone got any idea how I can accomplish this?
As said, I cannot accomplish this using normal means.
The page is build as:
Page 1, has a div, empty until the user clicks a button.
Page 2 loads a lot of content. When the user clicks on the button on page 1, this is loaded into the div.
When the user scrolls through the div (It has overflow set to auto) and ends at the bottom of the div it should load the next page or so to speak.
Thanks in advance! :)
There are some plugins which do this for you quite well.
http://www.webdeveloperjuice.com/2010/02/24/create-infinte-scroll-effect-using-jquery-with-demo/
http://www.beyondcoding.com/2009/01/15/release-jquery-plugin-endless-scroll/
EDIT: Some more up to date plugins:
https://github.com/fredwu/jquery-endless-scroll
http://www.infinite-scroll.com/infinite-scroll-jquery-plugin/
Use the JQuery.load() method to grab some html content and then insert it into an html element.
If you're using Paul Irish's Infinite Scroll, you can just specify a binder as one of the arguments where you have implemented infinite scroll, just like this:
$('.mydiv').infinitescroll({
binder: $(".name-of-div")
The binder is set to the document window by default, but if you specify it, you can have infinite scroll running simply inside one div on your page

Why some time jquery click event fails?

My page is divided into two parts vertically.Left part is like a menu section. Clicking on
any menu brings the proper data related to that menu in the right part of the page. I am
doing it using ajax call and the only div on the right part get refreshed. I am using jquery click event for that ..
say:
$("#left_part").click(function() { // ajax call here });
Now I have some more jquery action on the right part. (say Show hide some div which also work on click event.)
But when new div reloads on the right part those click event on the right part is not working.
But when I bind the click event it works.
say:
$("#some_right_part").click(function() {/ some hide show here )}; Not working
$("#some_right_part").bind('click', function(event){ // some hide show here )}; works fine
Important: When I am on fresh page (no ajax call yet to bring right part) $("#some_right_part").click(function() {/ some hide show here )}; works fine.
But what I know is: $().click(function) calls $().bind('click',function)
So, What is the reason behind this? Or What is the perfect way to solve such problem
Thanks
When you assign a click event via $().click when the page loads, the click event is bound to all matching elements. However, when you do the ajax call and recieve new markup - the new elements are not bound to the click event because they did not exist when you first called $().click.
There is a way to get a click event to be bound to all elements and all future elements that may be created. You must use the live method like so:
$('#elements').live('click', function() {
// do logic
});

Resources