?
Solved

Append to href using JQuery

Posted on 2014-11-10
2
Medium Priority
?
275 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 9

Accepted Solution

by:
Mukesh Yadav earned 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
Suggested Courses

762 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