JavaScript How to call javascript function on all elements with the same classname call javascript function from html,call javascript function from php,

I am trying to get a pop-up window to display when a user clicks on an element on my page. I have all the functionality for the pop-up working correctly; however, I am having trouble getting the function to pop-up for every element that matches the specified classname. My code is below:

<script>

        function descPop () {

            // Description pop-up 
        // Get the modal
        var modalprod = document.getElementById('myModalTwo');

        // Get the button that opens the modal
        var btnprod = document.getElementsByClassName("add-but")[0];

        // Get the <span> element that closes the modal
        var spanprod = document.getElementsByClassName("prod-close")[0];

        // When the user clicks on the button, open the modal 
        btnprod.onclick = function() {
            modalprod.style.display = "block";
        }

        // When the user clicks on <span> (x), close the modal
        spanprod.onclick = function() {
            modalprod.style.display = "none";
        }

        // When the user clicks anywhere outside of the modal, close it
        window.onclick = function(event) {
            if (event.target == modalprod) {
                modalprod.style.display = "none";
            }
        }   
    }

    </script>

Now, I know that currently this code is assigning index 0 to var btnprod; therefore, it will only pop-up when you click on the first element which matches the classname "add-but". How do I assign and access all elements, so that every tag with the classname "add-but" will pop-up the window?

Thanks

Solution:

function descPop () {

            // Description pop-up 
        // Get the modal
        var modalprod = document.getElementById('myModalTwo');

        // Get the button that opens the modal
        var btnprod = document.getElementsByClassName("add-but");

        // Get the <span> element that closes the modal
        var spanprod = document.getElementsByClassName("prod-close");

        // When the user clicks on the button, open the modal 
        for (var i = 0; i < btnprod.length; i++) {
            btnprod[i].onclick = function() {
                modalprod.style.display = "block";
            }
        }

        // When the user clicks on <span> (x), close the modal
        for (var i = 0; i < spanprod.length; i++) {
            spanprod[i].onclick = function() {
                modalprod.style.display = "none";
            }
        }


        // When the user clicks anywhere outside of the modal, close it
        window.onclick = function(event) {
            if (event.target == modalprod) {
                modalprod.style.display = "none";
            }
        }   
    }
Answer:1

If Jquery is not an option, you could iterate over the HtmlCollection to do it:

var btnprod = document.getElementsByClassName("add-but"); // <- this gives you  a HTMLCollection 
for (var i = 0; i < btnprod.length; i++) {
    btnprod[i].onclick = function() {
        modalprod.style.display = "block";
    }
}
Answer:2

you can change your code to the following:

$('.add-but').on('click', function() {
  $(this).css('display', "none");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="add-but">
  fafafafafa
</div>
<br>
<div>
  fafafaffafafaafafa
</div>
<br>
<div class="add-but">
  fafafafafafafaafafafaffafa
</div>
Answer:3

I intend to focus on usage of Directive Controller only. So, pl answer around the same error as mentioned as I suppose for each instance of directive usage, angular would go once in controller section ...

I intend to focus on usage of Directive Controller only. So, pl answer around the same error as mentioned as I suppose for each instance of directive usage, angular would go once in controller section ...

I am trying to develop a form abandonment heat mapper function newMarkup(id, uniqueHits){ var min = Math.min.apply(null,uniqueHits); var max = Math.max.apply(null,uniqueHits); var med = max / 2; for(...

I am trying to develop a form abandonment heat mapper function newMarkup(id, uniqueHits){ var min = Math.min.apply(null,uniqueHits); var max = Math.max.apply(null,uniqueHits); var med = max / 2; for(...

  1. data from google forms
  2. data from google maps
  3. data from google analytics
  4. data from google trends
  5. data from google sheets to bigquery
  6. data from google forms to spss
  7. data from google
  8. data from google searches
  9. data from google account
  10. restore data from google drive
  11. pull data from google spreadsheet to website
  12. restore data from google
  13. restore data from google account
  14. pull data from google spreadsheet to html
  15. import data from google sheets to excel
  16. get data from google sheets
  17. display data from google spreadsheet on website
  18. download data from google
  19. export data from google maps to excel
  20. export data from google analytics

I am trying to check/uncheck a jquery-ui checkboxradio widget programatically (i.e. without using the mouse). I've tried all manner of things but not joy. To reproduce the issue, choose a blank html ...

I am trying to check/uncheck a jquery-ui checkboxradio widget programatically (i.e. without using the mouse). I've tried all manner of things but not joy. To reproduce the issue, choose a blank html ...

  1. check uncheck jquery
  2. check uncheck jquery event
  3. check uncheck jquery checkbox
  4. radio check uncheck jquery
  5. jquery uncheck checkbox
  6. toggle check uncheck jquery
  7. checkbox check uncheck jquery event
  8. check uncheck using jquery
  9. check uncheck checkbox jquery not working
  10. check uncheck input jquery
  11. check uncheck all jquery
  12. check uncheck icheck jquery
  13. check uncheck checkbox jquery asp.net
  14. check uncheck all checkbox jquery
  15. check uncheck radio button jquery
  16. check uncheck checkbox using jquery
  17. multiple checkbox check uncheck jquery
  18. check uncheck all checkbox jquery datatable
  19. check/uncheck all with jquery child/parent
  20. check uncheck all checkboxes jquery in gridview

I have a page with an empty text input, an empty span and the following code: $('input').on('input', function(){ $('span').text(this.value); }); When I type in the input the same text appears in ...

I have a page with an empty text input, an empty span and the following code: $('input').on('input', function(){ $('span').text(this.value); }); When I type in the input the same text appears in ...

  1. when going back to school
  2. when going back to moon
  3. when going back to the gym
  4. when going back
  5. in going back to cali
  6. in going back to the start
  7. in going back to my roots
  8. by going back and forth
  9. by going back to your roots
  10. by going back to sleep
  11. breastfeeding when going back to work
  12. pumping when going back to work
  13. when we going back to school
  14. when we going back to the moon
  15. when you going back
  16. when time going back
  17. when clocks going back
  18. dreaming when going back to school
  19. when is nasa going back to the moon
  20. when is tiktok going back to musically