Solved

Append to href using JQuery

Posted on 2014-11-10
2
230 Views
Last Modified: 2014-11-11
I have two links on a page that when clicked open modal dialog box. In the box there are two links that I would like to append with the herf from the main link that was clicked. The modal box opens but I can't seem to get the href from the main link added to the herf's of the links in the modal box.

Or, if they are added, clicking out of the modal box, then back end causes links to be appended twice.

What am I doing wrong?

<a class="launchWebstore select-class" href="/p/product1">Visit our Store - Product 1</a><br />

<a class="launchWebstore select-class" href="/p/product2">Visit our Store - Product 2</a>

<!-- hidden modal-content -->
<div id="storeModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="chooseStore" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="chooseStore">Please Choose Your Store</h4>
      </div>
      <div class="modal-body">
          <div class="well">
              <a href="http://store.mydomain.com" class="store-URL" target="_blank" style="text-decoration: none;"><button type="button" class="btn btn-primary btn-lg btn-block webstore-button"><img src="/images/USAFlag.png" alt="United States" />&nbsp;United States</button></a>
          </div>
          <div class="well">
              <a href="http://canadianstore.mydomain.com" class="store-URL" target="_blank" style="text-decoration: none;"><button type="button" class="btn btn-primary btn-lg btn-block webstore-button"><img src="/images/CanadaFlag.png" alt="Canada" />&nbsp;Canada</button></a>
          </div>
      </div>
</div><!-- /.modal-content -->

<script type="text/javascript">
    $(".launchWebstore").click(function(event){
                
        event.preventDefault();
        $('#storeModal').modal();
        $(".webstore-button").click(function(event){
            $('#storeModal').modal('hide');
        });
        
        $("a.store-URL").each(function() {
           var _href = $(".launchWebstore").attr("href"); 
           $(this).attr("href", $(this).attr("href") + _href);
        });                     

    });
</script>

Open in new window

0
Comment
Question by:jej07
2 Comments
 
LVL 6

Accepted Solution

by:
Mukesh Yadav earned 500 total points
ID: 40434298
<script>
modalLinkHrefs = [];
$(document).ready(function(){
  $('.store-URL').each(function(index){
    modalLinkHrefs[index] = $(this).attr("href");
  });
  $('.launchWebstore').on("click", "", function(){
    var hrefToAppend = $(this).attr("href");
    $('.store-URL').each(function(index){
      $(this).attr("href", modalLinkHrefs[index] + hrefToAppend);
    });
  });
});
</script>

Open in new window

0
 
LVL 1

Author Closing Comment

by:jej07
ID: 40435001
Thank you very much for your help. Your solution worked perfectly.
I did add a separate function specifically for opening the modal.

<script type="text/javascript">
    $(".launchWebstore").click(function(event){
        event.preventDefault();
        $('#storeModal').modal();
        $(".webstore-button").click(function(event){
            $('#storeModal').modal('hide');
        });
    });                        
</script> 

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now