likwidmonster
likwidmonster

Reputation: 413

Javascript select div and change href inside

I am trying to modify the links in my page using JavaScript. I can select the div and the a element but cannot change the href.

Shopify is using a for loop to generate these objects and need each object to have a different link.

Here is the HTML:

<div id="tri-banner-1" class="banner-element banner-1 col-sm-4">
                <a href="collections/all"><div class="home-banner">                  
                  <img src='//cdn.shopify.com/s/files/1/1093/4220/t/2/assets/group_top_banner01.png?17711418137279289271' alt="">
                  <div class="banner-caption">
                    <div class="display-table">
                      <div class="display-tablecell">

                      </div>
                    </div>                        
                  </div>                 
                </div></a>                                     
              </div>  



              <div id="tri-banner-2" class="banner-element banner-2 col-sm-4">
                <a href="collections/all"><div class="home-banner">                  
                  <img src='//cdn.shopify.com/s/files/1/1093/4220/t/2/assets/group_top_banner02.png?17711418137279289271' alt="">
                  <div class="banner-caption">
                    <div class="display-table">
                      <div class="display-tablecell">

                      </div>
                    </div>                        
                  </div>                 
                </div></a>                                     
              </div>  



              <div id="tri-banner-3" class="banner-element banner-3 col-sm-4">
                <a href="collections/all"><div class="home-banner">                  
                  <img src='//cdn.shopify.com/s/files/1/1093/4220/t/2/assets/group_top_banner03.png?17711418137279289271' alt="">
                  <div class="banner-caption">
                    <div class="display-table">
                      <div class="display-tablecell">

                      </div>
                    </div>                        
                  </div>                 
                </div></a>                                     
              </div> 

Here is my JavaScript.

<script type="text/javascript">

  $(window).bind("load", function() {
      window.alert("Test");
      var banner1 = document.getElementById("tri-banner-1");
      var banner1Links = banner1.getElementsByTagName("a");
      banner1Links.href="facebook.com";
  });
</script>

Upvotes: 1

Views: 1661

Answers (2)

guest271314
guest271314

Reputation: 1

banner1.getElementsByTagName("a"); returns an HTMLCollection . You should either select the element using the index ; e.g.;

// set `href` of first `a` element in `bannerLinks`
banner1Links[0].href = "facebook.com";

or iterate the collection

for (var i = 0; i < bannerLinks.length; i++) {
  bannerLinks[i] = /* set `href` here */;
}

Upvotes: 2

christoz
christoz

Reputation: 56

banner1Links gets an array value so you need to loop in that array and for every array element you pass the value

// instead of banner1Links.href="facebook.com";

for(var i = 0; i < banner1Links.length; i += 1){
  banner1Links[i] = 'http://example.com';
}

Upvotes: 0

Related Questions