Solved

Append to href using JQuery

Posted on 2014-11-10
2
219 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
Comment Utility
<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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

743 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

12 Experts available now in Live!

Get 1:1 Help Now