Skip to content Skip to sidebar Skip to footer

Element Ids Are Not Being Recognised And Are Not Triggering Function

I'm trying to create simple selection option for users when selecting a particular font style. If they click on a font div, the background of that div should change color. The prob

Solution 1:

The problem: ID's are meant to be unique identifiers. You should be using classes if you want to identify multiple elements. For this reason, when you select by ID with $("#font"), jQuery (which uses native javascript's getElementById) will only return the first element it comes across that has that ID. This is because if the DOM is valid, it shouldn't find anymore instances of that ID and it would be a waste of processing to continue looking.

A solution: Remove the font ID's from your divs and replace them with a class instead, like class="font". Since you already have some classes identified, you can use multiple classes separated by spaces, like: class="font minecrafter". After doing this, you will be able to select your elements with the font class using this selector: $(".font")

Post a Comment for "Element Ids Are Not Being Recognised And Are Not Triggering Function"