0

I'm puzzled why my very easy script doesn't work. I want to write text into an input field when pressing a button, simple as that :)

Right now I have this:

<input type="button" class="plan-button" id="button15"  value="충전" />

and my jQuery:

$('#button15').click(function() {
  $('input').val($('input').val() + 'sample text');
});  

Without the click event, using only this line:

 $('input').val($('input').val() + 'more text');

This works fine. So the problem is the click event? I triple checked for errors... Help would be very much appreciated! Thanks!

EDIT: Here is the rest of the page... I excluded some of the identical table rows to make it easier to read.

 <script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){

$('#button15').click(function() {
  $('input').val($('input').val() + 'more text');
 });

});
</script>
<table id="mytable" border="0" style="margin-left: auto; margin-right: auto;">
    <tbody>
        <tr>
            <td style="text-align: center;">
                <div class="box"> <input type="button" class="plan-button" id="button15"  value="충전" /></div>
</tbody>
</table>
4
  • This will work, the problem is elsewhere. Commented Jul 22, 2016 at 20:42
  • Can you provide some more of you code? As said before, it will work this way. Commented Jul 22, 2016 at 20:47
  • Works fine, perhaps show more of the page contents so we can see where the problem is codepen.io/pjabbott/pen/rLvNoX Commented Jul 22, 2016 at 20:47
  • I just tried it in your code pen and it works. It is within a joomla page and the input which I want to write in is in a different module, maybe that is the problem. Very strange though that as soon as I remove the click event, everything works fine. Commented Jul 22, 2016 at 21:06

1 Answer 1

1

In the click, you don't say which input you want. If it's all, you need to do an each like this:

$('#button15').click(function() {
  $('input').each( function(){ 
      $.(this).val( $(this).val() + 'sample text')
  });
});

If you want add 'sample text' only in specific input, add a class like this:

 $('#button15').click(function() {
  $('.change').each( function(){ 
      $.(this).val( $(this).val() + 'sample text')
  });
 });
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.