I have a page with a selection of different AJAX calls, simple getJSON which updates the main results container <div id="results"></div>.
Example
<div id="results"></div> // all json is render here
<div id="content"></div> // div used outside of the results dataset
If somebody clicks on a link within any of the data items, the results are then updated so the <div id="content"></div> is positioned below the item clicked, like so:
<div id="results">
data
data (clicked)
<div id="content"></div>
data
data
</div>
This is good and exactly how its supposed to work.
Though the problem is, if a user now requests another dataset (getJSON) everything inside <div id="results"></div> is removed and replaced - including <div id="content"></div> which is needed for the next dataset.
So now, I only have
<div id="results"></div> // all json is render here
Iām wondering is there a way to save/keep <div id="content"></div> on the page and not be lost/removed when the results are updated?
Just realised I could maybe use .after AJAX success:
success: function (data) {
...
$('#results').html(html);
$('#results').after('<div id="content"></div>');
},
Would this be the best way?
The <div id="content"></div> container also has a lot of markup and html elements within. So can become a little messy pasting all this into the success method.
Yes and yesā¦ as for garbage collection, itās kept as long as the variable referencing that element can still be reached from anywhere in your code. For example, in this case
$('#my-button').click(function () {
var $content = $('#content')
$content.remove()
})
the element will get removed from memory when the callback finishes. But in this case
var $content = $('#content')
$('#my-button').click(function () {
$content.remove()
})
the reference is kept as long as the event listener is activeā¦ have a look here for details.
That would generally be possible too, but then you have the same element in the DOM twice without any needā¦ and an immediate problem would be the duplicate ID.
If the button wasnāt clicked, the #content would not be removed. Resulting in duplication when other json calls were called as the #content was initially outside of #results.
I was excited hoping to get everything working today
For some reason, even though the new #content was replaced in the correct area the click event wasnāt firing, or something wasnāt fitting together.
Iām going to build a test suite using a similar approach so you can see what the problem might be. Tricky one, just canāt figure out whatās going wrong
Will post back once I have everything built.
Also looking to build some sort of AJAX modular/function as page is becoming very saturated with DRY AJAX calls, only the parameters are changing. Probably open a new thread for this.
This is not an exact duplicate of my setup, though everything is working in a similar way.
After striping this down, the issue seems it could maybe be with the .more click event I have setup, the function that positions the #content under the .wrapblock divs.
In order
Click a couple of more links - this will position the #content under the row of divs which stays under the row/div also on resize.
Now with the #content active and showing, select another category from the dropdown - this will refresh the results
Click on the more links again, and youāll see the #content div always stays at the bottom
I need it to work as things do before we make the change with the dropdown.
And the data inside #content holder is just for viewing purposes, full list of people.
but when you replace the HTML of the results, those elements stored in $allBlocks wonāt be attached to the DOM any more, so your .filter() function wonāt work. So in this case, you actually do have to query for the blocks anew (or at any rate update the collection after the result list got populated).
Yes Iāll make note of this seems a lot cleaner.
Thanks in advance.
And sorry for my ignorance @m3g4p0p, but I just donāt understand how I can fit this together.
All the work is done within the click .more click event.
Do I need to add the logic from this into both AJAX success calls and still have this within the click event?
I ran a few tests but keep getting the error
Uncaught ReferenceError: position is not defined
Would be great if you could update/amend the Codepen
A little tricky to build the exact steps, lots of files running within a CMS not sure what is casuing this. Iāll see if I can fix it or build a test case to show.
It seems only the top 3 items have this issue and a second click makes the page jump to the top bypassing preventdefault. I currently have 3 items per row which has me thinking maybe something to do with the positioning.
But, just guess work really.
Could be other files conflicting. Though as the error says, pointing to .after($content);
Cool, was just wondering maybe you had ran into this issue before.
Oh I thought it would occur in the pen tooā¦ well generally speaking this error means that somehow the .wrapblock elements slipped into the $content. Under the hood, the jQuery .after() method uses .insertBefore(); a vanilla JS implementation might look roughly like this:
function after (node) {
this.parentNode.insertBefore(node, this.nextSibling)
}
And if node in the above snippet contains this.parentNode, youāll get that error as you canāt insert a node into itself.
But yeah I know reproducing a bug can be harder than actually fixing itā¦ ^^