Solved

Append to href using JQuery

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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)

770 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