3

Hi can I set 2 attributes to same element ? I want to run 2 functions when I outfocus on one item ? any idea if not any alternative ? thanks.

 listEl.setAttribute('onfocusout', `updateItem(${index}, ${row})`, 'onfocusout', `hideInputBoxOnElement(${row})`);
  • Set attribute only sets one attribute. You will need to use two calls (in this example it would only add 7 more characters) https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute – evolutionxbox Jan 07 '21 at 00:59
  • can you please elaborate more thanks. I want 2 things to happen after I onfouseout of the target element. –  Jan 07 '21 at 01:00
  • Check the first example in the link I posted – evolutionxbox Jan 07 '21 at 01:07
  • so I create a var selecting the button then var.setAttribute and double time the Attribute call.. –  Jan 07 '21 at 01:13

1 Answers1

3

I think you are looking for an event listener. Things like onblur, onclick, onmouseover, onmouseout, etc. are not attributes, but rather are inline event listeners that fire when that event occurs.

//select the myElement somehow, either using the element's ID, class, etc.
myElement.addEventListener('focusout', function() {
  updateItem(index, row);
  hideInputBoxOnElement(row);
});

You can have as many event listeners as you want in an element, so you can even do something like this:

//select the myElement somehow, either using the element's ID, class, etc.
myElement.addEventListener('focusout', function() {
  updateItem(index, row);
});

//select the myElement somehow, either using the element's ID, class, etc.
myElement.addEventListener('focusout', function() {
  hideInputBoxOnElement(row);
});

Just a suggestion, you might want to use the blur event instead of the focusout event. What's the difference, you ask? In web browsers, what's the difference between onblur and onfocusout? Basically, focusout is triggered when the element itself or any of its children lose focus, whereas blur only fires for the element itself.

It is often said that javascript event listeners that don't begin with the prefix on- is considered a better practice compared to starting with the prefix on-, but I personally don't think it makes much of a difference.

Endothermic_Dragon
  • 1,147
  • 3
  • 13