Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8877695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:31:39+00:00 2026-06-14T19:31:39+00:00

I am trying to append to a link with id a value that is

  • 0

I am trying to append to a link with id a value that is entered from the input text field. I came this far searching stackoverflow but id doesn’t work!

<script type="text/javascript">
 jQuery(function(){
   $("#txt_name").keypress(function() {
     var value = $("#txt_name").val();
     });
   $("a#coupon_link").attr("href", function(i) {
      return href + '&discount_code='.text(value); 
      });
});
</script>

and this is how the html looks like

<form>
  <fieldset>
    <input id="txt_name" type="text" value="discount" />
  </fieldset>
</form>
<a id="coupon_link" href="https://www.e-junkie.com/ecom/gb.php?c=cart&i=XXXXXX&cl=YYYYYY&ejc=2" target="ej_ejc" class="ec_ejc_thkbx" onClick="javascript:return EJEJC_lc(this);"><img src="http://www.e-junkie.com/ej/ej_add_to_cart.gif" border="0" alt="Add to Cart"/></a>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:31:40+00:00Added an answer on June 14, 2026 at 7:31 pm

    Fix your code like this :

    $(function(){
       $("#txt_name").keypress(function() {
         var value = $("#txt_name").val();
         var link = $("#coupon_link");
         var originalHref = link.attr('originalHref');
         if (!originalHref) {
              originalHref = link.attr("href");
              link.attr("originalHref", originalHref)
         }
         link.attr("href", originalHref + '&discount_code='+value);
       });
    });
    

    A few things to note :

    • never add anything to a selector when you’re targeting an element by ID
    • your value variable wasn’t in the same scope
    • the return of val can be directly concatenated, you don’t need to try to change it to text
    • you don’t need to pass a function to attr in your case
    • you’re trying to make the href grow with every key stroke. This is a bad idea. The solution I propose is to keep the original href
    • if you’re not sure the original href has yet some parameters (i.e. has ‘?’) you should test it (I let you do that)

    Overall a much cleaner solution wouldn’t be to change the link but to build the href on click on the link :

    $("#coupon_link").click(function(e) {
       location = this.href + '&discount_code=' + $('#txt_name').val();
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to append an image after the last input field in a
if i have a text field like this: <input type=text name=quantity id=txtquantity> what i
I am trying to append the text of a link inside a <li> to
I am trying to append a li element to a list with a link
I am trying to append some text to excel cell. For example if the
I'm trying to append some text to a variable in a Makefile for HP-UX's
I'm trying to append a string of svg elements to the dom. This is
i am trying to concat a query value into a link wwww.test/video (query value)
I am trying to overwrite a field of the AdmingeneratorGeneratorBundle and want to append
I am trying to translate the VBA code found in this link into IronPython.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.