Javascript is a language with thousands of frameworks and together with them are thousands of solutions to solve our problems and improve our code bases.
Suppose that built your page giving ids for tags with the goal to identify portions of your page. Now your client asks you to update given tags with specific ids with some clever behavior, but the question is that some of them may not be present since your page is built dynamically based on server data.
Now if you’re able to use JQuery, which was my case, you can use a selector to evaluate the attribute value and fetch the non-empty ones. Even further you can also ensure that the tag has the attribute.
Given any js code you can do
First part [id!='']
demands for tags in which id has some value when
exists but doesn’t require the tag to have the id attribute.
The second part [id]
demands tags which have the id attribute but doesn’t care
about its value.
Combined they fetch only tags that have the id which values aren’t empty.
In that sample above, using the following selector
We would obtain the following object with three divs
This can be seen here, in JQuery’s documentation.