Solved

Append to href using JQuery

Posted on 2014-11-10
2
262 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 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

687 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