0

I wanted to add the resize event to an element (I know you can't add resize to a specific element, but it would be fine if the JS-function would be triggered on a resize of any kind - not the specific element).

I was thinking about this:

<div onresize="wrapperResize();"></div>

This somehow doesn't work (Safari user here). This however works:

<div onclick="wrapperResize();"></div>

Currently wrapperResize() only holds a simple console.log(). What could I do about this?

PS: This works great (traditional checked), what do I even do different?

EXTRA: The div has a fixed height and 33% of the body element wide. Even though the event should fire, when the window is resized I thought this may be the cause.

5
  • there is no native "resize" event because elements don't resize natively. if your stuff is resizable you must be using a js plugin, in which case you should consult the docs for said plugin. Mar 4, 2016 at 20:57
  • What is this then? Mar 4, 2016 at 20:58
  • Supported HTML tags: <body>. Did you try to add it to body instead of your div? Mar 4, 2016 at 21:00
  • oh cool, didn't know about that new feature. according to your w3schools page it fires when the page is resized, not the element itself, so it doesn't really make sense to put the attribute on some arbitrary elemnt, do it with js like in the answer or put it on the body tag. Mar 4, 2016 at 21:01
  • @SerhiyChupryk ... I'm stupid. Thank you really much. This works, it would be good to have it on the div though. Are there good alternatives? Mar 4, 2016 at 21:01

2 Answers 2

2

I think you should set onresize handler on window and do it in javascript, not inline html.. Like so

window.onresize = yourFunction
2
  • I'll try, wait a second. Mar 4, 2016 at 21:02
  • Sure, I already wanted to, but had to wait for a few minutes. Mar 4, 2016 at 21:14
2

You cannot attach a onresize event on a <div> container. It is only available for window for most of the browsers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.