• 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" "https://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)
  • Hi ??

    Here is some example code: 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

    (@roelfk7)

    wow thank you so much will try it out now.

    Thread Starter Roelf Keulder

    (@roelfk7)

    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

    (@roelfk7)

    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 3 years, 3 months ago by Roelf Keulder.

    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:

    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

    (@roelfk7)

    Thank You

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Quick Populate Buttons jquery/java’ is closed to new replies.