0

I want to link a callback to each input (type text) with class 'liveVariable':

$(".liveParameter").keyup(function(){
    var callBackString= 'skp:change-'+$(this).attr('id')+'@'+$(this).attr('value');
    window.location = callBackString;               
});

However, this gives me a strange error. The value passes is the initial value of the input field (every time the event is fired).

I don't understand why this doesn't work. I dont think I have a closure, the local variable callBackString is created in the called function. So it should be created every time over?

How can I solve this in an elegant manner? (I would not like to create a call for each input field, cause I have many live parameters..)

5
  • 2
    Change $(this).attr('value') to $(this).val() Commented Sep 27, 2013 at 8:46
  • 1
    Also, just for the sake of my OCD, change $(this).attr("id") to this.id Commented Sep 27, 2013 at 8:49
  • Interesting @Archer...benefits? Commented Sep 27, 2013 at 8:49
  • 5
    @ChrisKempen The benefits of not constructing a huge jQuery object just to get the id of the element which can be accessed directly with this.id? :) Commented Sep 27, 2013 at 8:52
  • 1
    But the $ looks so pretty! xD Commented Sep 27, 2013 at 8:53

2 Answers 2

1

Change in your code

$(this).attr('value'); // get the initial value

$(this).val(); // get the current and updated value

http://jsfiddle.net/FjgLB/

Sign up to request clarification or add additional context in comments.

Comments

1

Try changing:

var callBackString= 'skp:change-'+$(this).attr('id')+'@'+$(this).attr('value');

...to:

var callBackString= 'skp:change-'+$(this).attr('id')+'@'+$(this).val();

More detail on the .val() function can be found on the jQuery docs website here.

Edit:

For the sake of elegance, performance, and comment contribution, the following solution would ultimately be the best:

$(".liveParameter").keyup(function(){
    var input = $(this);
    var callBackString = 'skp:change-' + input.attr('id') + '@' + input.val();
    window.location = callBackString;
});

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.