I have the following code which simply focuses a DOM element.
const focus_item = id => {
let element = document.body.querySelector(`.item[data-id="${id}"]`)
if(element) element.focus();
}
I'm looking for a way to make this code into one line, something like this:
const focus_item = id => document.body.querySelector(`.item[data-id="${id}"]`).focus();
The problem is that I really need the if statement as well, because it's not guaranteed that the DOM element exist. Is there any way to only call focus() if the selector returns an element?