I'm trying to grab all the ids which have a common class name and storing them as an array using the following code (this works correctly):
var ids = $('.idSelect').map(function() {
return $(this).attr('id');
}).get();
However, I then want to define 'btn' to multiple ids, so that any id can trigger the rest of my JavaScript, however it doesn't seem to be working with the following code snippet.
var btn = '#' + ids.join(', #');
From the chrome console, it seems to me that it is acting as one big string.
Edit:
The rest of the code -
// Get the modal
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var ids = $('.idSelect').map(function() {
return $(this).attr('id');
}).get();
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
so that any id can trigger the rest of my JavaScriptHow? Are you trying to add click listeners to them, or something? Can you post that code so we have a clearer idea of what you're trying to accomplish?