Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Jquery – how to add a click event for each button

Using the below jquery, I am able to find each button with the id, what I can’t get to happen is when I lick the button to actually load the URL. How can I modify the jquery so when I click the button it will before the load?

<script>
$(document).ready(function() {
    var url = '@Url.Action("_Research", "Dashboard")';
    const buttons = document.querySelectorAll('.btn');
    buttons.forEach(
        function (currentBtn) {
            alert(currentBtn.id);
            currentBtn.click(function() {
                $('#researchDiv').load(url, {
                    ticker: currentBtn.id
                })
            });
        }
    );
});
</script>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Since you’re using jQuery, you don’t need a loop. The jQuery event binding method automatically loops over all the elements. Then you can use this to refer to the button that was clicked.

$(document).ready(function() {
  var url = '@Url.Action("_Research", "Dashboard")';
  const buttons = document.querySelectorAll('.btn');
  $(".btn").click(function() {
    $('#researchDiv').load(url, {
      ticker: this.id
    })
  });
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading