0

I have this field in one form:

<input type="text" name="promocode" id="promoCode" value="" style="width:24%" maxlength="5"> 

and this field in another form:

<input type="hidden" name="custom" class="promo" value="" />

How can I do, so whenever the value of #promoCode is changed, it will also change the value of .promo

Currenlty, I have this:

var promo = $("#promoCode").val();
$("#promoCode").change(function(){
    $('.promo').val(promo);    
});

But that doesn't work.

4
  • is your code wrapped in a dom ready callback? Commented Apr 1, 2012 at 13:46
  • Probably #echo should be .promo or input[name="custom"]. Commented Apr 1, 2012 at 13:46
  • @Engineer. Where did you see #echo? Commented Apr 1, 2012 at 13:51
  • @gdoron It was $('#echo').val(promo); before editting . Commented Apr 1, 2012 at 13:52

1 Answer 1

3
$(function(){
    $("#promoCode").change(function(){
        $('.promo').val(this.value);    
    });
});
  • Make sure the DOM is ready when you attach your callback to the change event
  • this inside callback is the element the event attached to.
  • If you cache #promoCode value outside the change callback it will be out of sync right after the first change! use this.value inside the callback just like I did.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.