0

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?

2
  • 1
    $('#' + me ).val('something for you'); Don't worry, JQuery will do the rest - selector will be recognized. No need for quotes. Commented Aug 29, 2015 at 19:56
  • ^^ thx @nevermind works good Commented Aug 29, 2015 at 20:01

4 Answers 4

1

Try this :

$(document).ready(function(){
  $('body').click(function(){
    var me=$('.update-form').attr('data-which');
    $('#' + me).val('something for you');  
  });
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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');  
    });  
});

Comments

0

You can try with this

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

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.