html - Javascript: Change <a href> to match <img src> multiple times on a page -


i'm attempting write script take 'img src' , copy 'a href'. trick page i'm working on photo gallery, multiple images on page script needs work on. needs happen page loaded. here's html:

<!-- album row 1 --> <div class="col-md-6 col-sm-6 album">   <a class = "img-container-alt" href="**this_is_where_the_imgsrc_below/will_end_up.jpg**" data-gallery>    <div class="photo-overlay"></div>     <figure>      <img class="img-responsive album-img" src="**this_is_what_/i_would_like_copied_above.jpg**" alt="">     </figure>   </a> </div>  

the trick is, said, getting run through multiple divs identical 1 , dynamically grabbing each img src , copying a href above within div. here's js i've done far:

    <!-- script --> <script type="text/javascript"> window.onload = function abc() {     console.log('hello')     var copyto = document.queryselectorall('.img-container-alt');     var copy = document.queryselectorall('album-img');     (var = 0; < copyto.length; i++) {         var url = copy[i].src;         copyto[i].href = url;     } } </script> 

when run that, error: uncaught typeerror: cannot read property 'src' of undefined, referring var url = copy[i].src;. i'm not sure if script best option i'm trying do, i'm stumped now. i'm relatively new js, appreciated. in advance. if need clarify anything, please let me know.

getqueryselector returns nodelist of element objects suspect need use .getattribute("src") instead.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -