I have been trying all day & searched everywhere for a solution. It hasn't been really successful, and honestly after years browsing stackoverflow that's the first time I post looking for help. :-)
I recently implemented a strict CSP. I forked JSONscriptRequest class of Jason Levitt to secure more the dynamic tag (http://www.geonames.org/export/jsr_class.js) with nonce. I appreciate using this because I really customized and used it a lot (a lot of modules depends on it).
Originally, the script was outputing this in HTML :
<div class="suggestions" id="pcId0" onmousedown="suggestBoxMouseDown(0)" onmouseover="suggestBoxMouseOver(0)" onmouseout="suggestBoxMouseOut(0)">...</div>
<div class="suggestions" id="pcId1" onmousedown="suggestBoxMouseDown(1)" onmouseover="suggestBoxMouseOver(1)" onmouseout="suggestBoxMouseOut(1)">...</div>(and so on, depending on the number of results.)
...
They all have the same class (suggestions). I know i can do this to replace the inline event handlers, but without any parameters :
document.getElementById("placeInput").addEventListener("input", postalCodeLookup)
I tried doing this
document.getElementById("placeInput").addEventListener("change", function() {
var suggestions = document.getElementsByClassName('suggestions');
console.log(suggestions);
for (i = 0; i < suggestions.length; i++) {
suggestions[i].addEventListener("mousedown", suggestBoxMouseDown([i]));
suggestions[i].addEventListener("mouseover", suggestBoxMouseOver([i]));
suggestions[i].addEventListener("mouseout", suggestBoxMouseOut([i]));
}
});`
Annnd it has been a complete failure again.
I don't know how to pass variable to my functions as easily that we could before.
Concerned functions :
// remove highlight on mouse out event
function suggestBoxMouseOut(obj) {
document.getElementById('pcId'+ obj).className = 'suggestions';
}
// the user has selected a place name from the suggest box
function suggestBoxMouseDown(obj) {
closeSuggestBox();
var postalcodeInput = document.getElementById("postalcodeInput");
var placeInput = document.getElementById("placeInput");
var latitude = document.getElementById("latitude");
var longitude = document.getElementById("longitude");
postalcodeInput.value = postalCodes[obj].postalCode;
placeInput.value = postalCodes[obj].placeName + " (" + postalCodes[obj].postalCode + ", " + postalCodes[obj].countryCode + ")";
latitude.value = postalCodes[obj].lat;
longitude.value = postalCodes[obj].lng;
}
// function to highlight places on mouse over event
function suggestBoxMouseOver(obj) {
document.getElementById('pcId'+ obj).className = 'suggestionMouseOver';
}
Does anyone has an idea ?
Soem quick ideas to refactor the code:
Implement rules for classes
.suggestionsandsuggestionMouseOverwith.sugestionsand the:hoverpseudo class selector in CSS:This should remove the need to monitor
mouseenterandmouseleaveevents on.suggestionclass elements simply to change styling. (Notemouseoverandmouseoutcan fire over when mousing over child elements of the hover class.)Generate a
data-attribute on HTML elements to record the index number of the suggestion. Currently this implemented as a numeric suffix on theidattribute value which, while remaining a possibility, is not the cleanest solution:Although event processing could be delegated by providing a single event on a common parent element and looking at the
targetattribute of events fired, the straight forward approach may still be to stay with an event listener on eachdivelement: