jQuery resize handler does not operate on each element - javascript

Edited since original posting
I have written a resize handler that listens for a parent div being resized (as a result of an ancestor container div being dragged).
The handler relies on the iframe method described here, which I had employed in my own code: Resize on div element
I need to know that my divs have been resized as a result of the parent div being resized, and then do something as a result. Note that this is not a window/browser resize event.
When only one of my divs is on the page, it works fine. When multiples are used, only the last one is affected by my script. I cannot figure out why and am now asking for help in figuring this out.
This has all come about during my attempts to scope the code so that it does properly apply itself to each one of my affected divs in turn.
Fiddle here: https://jsfiddle.net/GrumJim/xpvt214o/939643/
The offending code begins with:
$('.rsg_testtiles_display_macro_container').each(function(index) {
There are two divs in my markup, but only the second one is processed (12 outputs to the console represent the 12 child divs, but the five child divs in the first div are ignored) and I don't know why.
All help much appreciated.

It seems like you shouldn't actually iterate over the $(this) instance, since is already referring to a single element.
Try to remove this line:
$(this).each(function() {
(and the appropriate closing of the function & parenthesis...)

Mark Scultheiss's comment to the original question had the answer, thanks for letting me work that one out.
I was most definitely calling things in the wrong order, particularly where I was assigning this to the var. I have solved this by properly calling the on, then the each, then assigning this to the var and working through each of my divs in turn.
I have far to go on my JavaScript journey, but you put me on the right path there. Thanks folks!

Related

JS : How to get notified after insertBefore()?

Is there a way to get notified, after inserting an element into the DOM with insertBefore(), when this element becomes actually visible/available to user ? Especially to start applying CSS transforms on it ?
Complete problem
Forgive me if this question is recurrent, I didn't find a suitable answer so far. I'm trying to implement a custom popup dialog system on a website of my own, similar to SweetAlert or some other products.
I would like to apply some special effects when this popup shows up, such as a progressive darkening of the background, as well as a slow vertical motion on the box itself.
To achieve all of this, I spawn one big, fixed div element covering the whole screen (the background) and containing the popup box. When I need it, I first insert this element as body's first child, tagging it with a special invisible class. Once inserted, I remove this invisible class from the element and let the CSS rules do the magic.
The problem is that even if removed the class after having inserted this element, this one will be rendered only when the Javascript function leaves, hence directly in its final state.
When doing this on a complete initial page, the load event helps. I now would like to do the same on an existing page.
As always, I'm interested on both solutions to this (potentially XY) problem: if there's a better way to do it, I'll be happy to discover it, but I'm still interested in solving this particular situation anyway.
Thanks in advance to everyone.
EDIT: currently performing tests on Firefox 82.0.2
Thanks to comments above, here's a valid solution to both exposed problems:
"Mutation Observer", as well as former "Mutation Events" (now deprecated) are the best way to get notified when something is inserted. It won't help with animations issues, though, because it's still not guaranteed to be rendered yet at this time ;
Rather than applying a class then another to perform a transition, it's better to define a regular animation using #keyframes that plays only once. It's guaranteed to be played when the object appears, by definition.
Many thanks to "Pomax", F4st3r and epascarello for their help.

How to move a DIV based on a mouse click in Javascript?

I have a testbed with four simple content DIVS which I would like to change the position of when each of them is clicked. Link below.
http://christopherwynne.com/tattoo/
I have performed similar functions with other sites in the past, and I feel like I am missing something fundamental. I am still new to Java, and have always had issues getting anything to work initially.
Any help with this issue would be greatly appreciated.
Your jQuery click handlers are not quite right. You're using:
$("galleryWrap")
but it should actually be
$("#galleryWrap")
to select the element by its id.
Once you've selected them properly you can do something like add a class which will attach the left margin of 205 you want.
Likewise you have a few other places where you're not selecting the elements properly. If you try selecting elements with these commands in the console you'll see jQuery can't find them.
An example: a command like $('div') selects all <div>s on the page, whereas if you wrote $('#div') you would select all elements with id="div".

KnockOut binding breaks after moving DOM element

I have a web app built on KO and for the most part it has been a god-sent. However, I have one very frustrating problem.
When I move an element with jQuery from one spot in the DOM to another, the bindings seem to randomly break. Sometimes they survive the move, sometimes they don't. Anyone know what might be causing this? I wish I could give a specific example, but I can't seem to re-create it in a simple case (for a fiddle) and it truly is random (3 in 10 tries).
Is there a way to refresh the bindings in an element?
Cheers,
Had a similar issue. It was happening for me when I was moving the DOM element before applying bindings.
Make sure that all the applyBinding calls are made before you move the DOM element.
That is about all the help I can give without a code sample.
Perhaps try using ko.cleanNode to clear the bindings from the moved element, then ko.applyBindings( model, element ) to rebind them?
See also How to clear/remove observable bindings in Knockout.js?.

Using jquery to add/remove class but elements are not redrawn to reflect

I have two elements (think of two buttons side by side). I dynamically toggle the class "focusd" to change the highlighted effect. However, there's a quirk it doesn't always get redrawn and/or inserted in the DOM. For example, if in chrome I do console.log, I see the class changes (I'm using removeClass/addClass in jquery). But if I go to the Elements tab in the inspector, it shows the classes from before (and in fact, I'm not seeing the redrawing reflecting the toggling of the classes.)
I tried setting the parent div to display none then back to block but that didn't work. It's a "one off" modale screen, so efficiency doesn't matter so I've resorted to this hack where I essentially copy the parent's innerhtml, remove and reinsert the element. Horrible!
// Not sure why I need this hack. But if I don't, the buttons don't seem to get redrawn
var htm = jQuery(".rdata_container").html(); // copy the innerhtml
jQuery(".rdata_container").empty(); // empty and then append back
jQuery(".rdata_container").append(htm);
This seems like a specific quirk that someone must have ran into (I hope). If so, I'd love to know why my changes aren't reflected.
EDIT
Code posted here:
http://jsfiddle.net/roblevintennis/JCZnf/
you can use setTimeout when you are doing the other operation on the element, so for example:
$elm.addClass('hide')
setTimeout(function(){
$elm.removeCalss('hide')
},0);
Or you could force a repaint like so:
$elm.addClass('hide')
$elm.scrollTop; // forces a repaint (might be expensive for large amount of items)
$elm.removeCalss('hide');
These tricks will force the browser to re-draw the change, because there are two things happening here and the browser logic just combines them into one, which isn't the desired behavior.
Not directly an answer to your question, but you can use jQuery's toggleClass function to simplify your code.
Here's an updated version that uses toggleClass() and jQuery 1.6 and AFAICT works fine.
http://jsfiddle.net/JCZnf/7/

jQuery plugin scrollTo works only from console

Im having an issue with scrollTo on newly created divs Im adding to the DOM via append(). For some reason I cant scroll to where I want from within the code, it only works from console command or if I add a button and call the scroll event that way.
Code:
$('#history').scrollTo('max');
Only works from console or attached to a button.
Edit: The history div has overflow:auto, not sure if that's causing it.
Just a guess, but are you calling "scrollTo" immediately after appending your new element to the DOM?
If that is the case, then the DOM layout likely hasn't had a chance to get recalculated. Relative offset values between elements aren't yet updated until after the current script processing finishes.
Two possible workarounds:
1. setTimeout("$('#history').scrollTo('max')", 1); // Call this after you append your element. This will allow the stack to unwind and update the DOM positions
2. $('#history').scrollTo(0, 9999); // Where "9999" is a value far larger than the actual height of the control.
This would indicate that you're trying to attach scrollTo() to elements which aren't actually existing at the time that you're calling the function on them. The fact that it works in the console would suggest this.
If that's the case, one way would be to use e.g. a library like livequery or then simply call the scrollTo() function on these elements once they have been appended.

Categories