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

how to convert this jquery into javascript?

I have been using this jquery code and I want to convert this into javascript. I have recently started javascript and I have a little knowledge in jquery

<script src="https://code.jquery.com/jquery-2.2.4.js"></script>
<script type="text/javascript">
  $(document).ready(function () {
    $(".row a").click(function (e) {
      e.preventDefault();
      $(".imgBox img ").attr("src", $(this).attr("href"));
    });
  });
</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

The equivalent in vanilla JavaScript is (I added some HTML to make it work):

document.addEventListener('DOMContentLoaded', function () {
    document.querySelectorAll(".row a").forEach(function(elem) {
        elem.addEventListener("click", function (e) {
            e.preventDefault();
            document.querySelector(".imgBox img").src = elem.href;
        });
    });
});
<div class="row">
    <a href="https://dummyimage.com/600x120/000/fff">link 1</a>
</div>
<div class="row">
    <a href="https://dummyimage.com/600x120/00f/f88">link 2</a>
</div>
<div class="imgBox"><img src=""/></div>
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