Results 1 to 3 of 3

Thread: Append to an href using js/ jQuery

  1. #1
    Join Date
    Aug 2010
    Posts
    86
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Append to an href using js/ jQuery

    Good morning,

    I'm trying to append a couple of parameters to an href and can't get it to work - the parameters just don't get appended.

    Here's the html showing a button the user clicks which launches a js script that validates the users responses to several questions. If the responses are valid the js then updates a couple of variables - blapp and tspapp. These are then appended to an href and the link is clicked by js. Everything seems to be working except that the parameters are not appended to the href.

    I tried two different ways, one was to use an embedded script with document.write, that didn't work and the other was to use jQuery append and that didn't work either...sigh.

    Here's the html showing the button and immediately below it the document.write code that I commented out and below that the link that I tried to append using jQuery.

    Code:
    <input id="continue" type="button" value=" Continue " onclick="checkAnswers();" />
    <!--     <a id="appLink" href="http://www.hollisterairshow.com/rally/vendorApp.php?<script>document.write("blapp="+blapp+"&tspapp="+tspapp)</script>"></a>  -->
       <a id="appLink" href="http://www.hollisterairshow.com/rally/vendorApp.php?"></a>
    Here's the js that validates responses and updates the values of the two parameters

    Code:
     <script> 
      function checkAnswers() {
    	  if(selling === "omitted" || tsp === "omitted" || (selling === "yes" && curbl === "omitted")) 
    	 {
    		dispMessage();
    		return;
    	 };
    	 alert("All OK so set up link to vendor app");
    	 if (selling === "yes" && curbl ==="no") {
    		 blapp = "yes";
    	 }
    	 else {
    		 blapp = "no";
    	 };
    	 if (tsp === "yes") {
    		 tspapp = "yes";
    	 }
    	 else {
    		 tspapp = "no";
    		 }
      	alert("blapp="+blapp+" tspapp="+tspapp);
    	var params = "blapp="+blapp+"&tspapp="+tspapp;
    	alert("params = " + params);
    	$( "#appLink" ).append(params);
    	document.getElementById('appLink').click();
    	}
      function dispMessage() {
    	   alert("Please check your answers and press <Continue> again");
    	   return;
      } // /checkAnswers
      </script>
    The complete page can be seen here http://www.hollisterairshow.com/rally
    Thanks for any assistance you can provide.

    Tony

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Using document.write after page load can have unpredictable consequences. And that's not how append works. And because only a few browsers support it, you shouldn't be changing and clicking links via javascript in order to change the page. Rather, just use javascript directly to change the page. So here:

    Code:
    	$( "#appLink" ).append(params);
    	document.getElementById('appLink').click();
    I would do instead:

    Code:
    	window.location.href = 'http://www.hollisterairshow.com/rally/vendorApp.php?' + params;
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Aug 2010
    Posts
    86
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Thanks John, you make this look easy. I must have spent several hours trying to get it to work....sigh.

    Thanks again.

    Tony

Similar Threads

  1. Replies: 4
    Last Post: 08-26-2012, 02:21 PM
  2. Resolved IE < 9 jQuery append / appendTo
    By ggalan in forum JavaScript
    Replies: 2
    Last Post: 10-17-2011, 11:43 PM
  3. jQuery Append Trouble in IE7.
    By fr600 in forum JavaScript
    Replies: 6
    Last Post: 08-11-2011, 01:31 PM
  4. jquery callback href
    By ggalan in forum JavaScript
    Replies: 3
    Last Post: 12-15-2010, 02:07 AM
  5. Replies: 1
    Last Post: 09-20-2008, 02:36 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •