Title: Quick Populate Buttons jquery/java
Last modified: August 11, 2021

---

# Quick Populate Buttons jquery/java

 *  [Roelf Keulder](https://wordpress.org/support/users/roelfk7/)
 * (@roelfk7)
 * [4 years, 9 months ago](https://wordpress.org/support/topic/quick-populate-buttons-jquery-java/)
 * Good day I hope you can help me
 * I’m creating a custom plugin and I would like to add 3 Buttons to change the 
   value in a textbox field.
    These 3 buttons has values attached to them but it
   need to do a calculation by adding their value to a existing value/variable and
   then fill the field with the result I have written a JavaScript that works but
   because the form name and textbox name has dashes in them I cant get it to work,
   so I think I need a jQuery script instead, but I have no idea how to make it 
   work. Any help will be appreciated
 * Here is an example f code I have written that works on in its own not in my plugin,
   just to give you an idea what I want to accomplish:
 *     ```
       <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
       <html lang="en-GB"> 
        <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
        <title>Button Form Populate</title> 
        <script type="text/javascript">
       function transferField(someval) { 
         document.groovyform.simba.value = someval; 
         } 
       </script>
       </head> 
       <body> 
        <form name="groovyform">
       <input type="text" id="simba" value="">
       <input type="reset" value="Reset">
       </form>
   
       <input type="button" onclick="transferField(this.value)" value="1000">
       <input type="button" onclick="transferField(this.value)" value="500">
   
        </body> 
       </html>
       ```
   

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [corenominal](https://wordpress.org/support/users/corenominal/)
 * (@corenominal)
 * [4 years, 9 months ago](https://wordpress.org/support/topic/quick-populate-buttons-jquery-java/#post-14757583)
 * Hi 🙂
 * Here is some example code: [https://jsfiddle.net/7Lx5puhb/](https://jsfiddle.net/7Lx5puhb/)
 * I think it meets this:
 * > These 3 buttons has values attached to them but it need to do a calculation
   > by adding their value to a existing value/variable and then fill the field 
   > with the result
 * Is it close to what you are looking for?
 *  Thread Starter [Roelf Keulder](https://wordpress.org/support/users/roelfk7/)
 * (@roelfk7)
 * [4 years, 9 months ago](https://wordpress.org/support/topic/quick-populate-buttons-jquery-java/#post-14765677)
 * wow thank you so much will try it out now.
 *  Thread Starter [Roelf Keulder](https://wordpress.org/support/users/roelfk7/)
 * (@roelfk7)
 * [4 years, 9 months ago](https://wordpress.org/support/topic/quick-populate-buttons-jquery-java/#post-14765869)
 * Thank you so much corenominal, it will surely work
 * I have one more question: I have a variable called $current-price, which was 
   generated earlier and I want them if they press the buttons it must add the value
   of the button + $current-price value and place the result in the text field.
 * I can’t seem to figure that bid out yet.
 *  Thread Starter [Roelf Keulder](https://wordpress.org/support/users/roelfk7/)
 * (@roelfk7)
 * [4 years, 9 months ago](https://wordpress.org/support/topic/quick-populate-buttons-jquery-java/#post-14766199)
 * I have hanged the script as follow, but nothing happens:
 * <script>
 * $(‘.btn’).on(‘click’, function(e){
 *  e.preventDefault();
 *  // Get val of btn
    let a = parseInt( $( this ).val() );
 *  // Get val of form imput
 *  var b = ‘<?php echo $current-price; ?>’;
    console.log(b);
 *  // Sanity check on b to make sure we are working with a number
    if( isNaN( b))
   b = 0;
 *  // Do the addition and place value in form input
    $( ‘#price-val’ ).val( a +
   b );
 * });
 * </script`
    -  This reply was modified 4 years, 9 months ago by [Roelf Keulder](https://wordpress.org/support/users/roelfk7/).
 *  [corenominal](https://wordpress.org/support/users/corenominal/)
 * (@corenominal)
 * [4 years, 9 months ago](https://wordpress.org/support/topic/quick-populate-buttons-jquery-java/#post-14766252)
 * I’m not sure if this will fix your issue, but `$current-price` is not a valid
   PHP variable name. Hyphens are not allowed. According to [PHP.net](https://www.php.net/manual/en/language.variables.basics.php):
 * >  A valid variable name starts with a letter or underscore, followed by any 
   > number of letters, numbers, or underscores.
 * Try changing it to something like `$current_price`.
 *  Thread Starter [Roelf Keulder](https://wordpress.org/support/users/roelfk7/)
 * (@roelfk7)
 * [4 years, 9 months ago](https://wordpress.org/support/topic/quick-populate-buttons-jquery-java/#post-14767994)
 * Thank You

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Quick Populate Buttons jquery/java’ is closed to new replies.

## Tags

 * [forms](https://wordpress.org/support/topic-tag/forms/)
 * [javascript](https://wordpress.org/support/topic-tag/javascript/)
 * [jquery](https://wordpress.org/support/topic-tag/jquery/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 4 participants
 * Last reply from: [Roelf Keulder](https://wordpress.org/support/users/roelfk7/)
 * Last activity: [4 years, 9 months ago](https://wordpress.org/support/topic/quick-populate-buttons-jquery-java/#post-14767994)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
