timpone
timpone

Reputation: 19939

how to update an input variable in jquery

I'm trying to update an input value based upon a user clicking a button. I have the following:

html

<input id="my-data-1" value="here is a value" />
<button data-which="my-data-1" class="update-form">update</button>

jquery

$(document).ready(function(){
  $('body').on('click','.update-form', function(){
    var me=$(this).attr('data-which');
   // this doesn't work, how would I update it?
    $("'#" + me + "'").val('something for you');  
  });
});

but it isn't working with error:

Uncaught Error: Syntax error, unrecognized expression: '.update-form'

How would I make this work?

Upvotes: 0

Views: 74

Answers (4)

Akshay Chawla
Akshay Chawla

Reputation: 613

You can try with this

$("#" + me).val('something for you');
});

Upvotes: 0

siracoj
siracoj

Reputation: 101

Looks like an issue with how you are selecting the button, you could try a more guarenteed way to select it:

$(document).ready(function(){
    $('body').find('.update-form').on('click', function(){
        var me=$(this).attr('data-which');
        $("#" + me).val('something for you');  
    });  
});

Upvotes: 0

Matthew Dunbar
Matthew Dunbar

Reputation: 426

You don't need the single quotes on the line where you set the new value. You want:

$("#" + me).val('something for you');

Here's the fiddle

Upvotes: 0

saruftw
saruftw

Reputation: 1164

Try this :

$(document).ready(function(){
  $('body').click(function(){
    var me=$('.update-form').attr('data-which');
    $('#' + me).val('something for you');  
  });

Upvotes: 1

Related Questions