0

I have this code:

<div class="cc-selector col-md-3">
<form method="POST" action="<?php echo base_url() . 'home/cart_finish/go'; ?>" class="checkout-form">
    <input type="hidden" name="payment_type" value="omise">
    <script src="https://cdn.omise.co/omise.js.gz"
        data-key="<?php echo $omise_public_key_test; ?>"
        data-image=" "
        data-frame-label="Acme"
        data-button-label="<?php echo translate('pay_now');?>"
        data-submit-label="<?php echo translate('submit');?>"
        data-location="no"
        data-amount=" "
        data-currency="us"
        >

      var total = $('#grand').html();
      $("[data-amount]").attr('data-amount', total);
     </script>  
</form>

I want to set a value to the "data-amount" attribute. I tried with the above code, but it doesn't work.

Note: Omise is a payment gateway similar to Stripe. That code will render a pay button. I have to had the data-amount set before its render. That's why I cannot wait for the $document to be ready. I hope it makes sense.

6
  • 2
    scripts with src attribute are not evaluated Commented May 18, 2017 at 6:21
  • If a script element has a src attribute specified, it should not have a script embedded inside its tags. Commented May 18, 2017 at 6:22
  • Ad id to it and use that id with in your code also put your code in another script tag not in that one which call cdn.omise Commented May 18, 2017 at 6:24
  • What is expected result of setting data-* attribute at <script> element? Commented May 18, 2017 at 6:27
  • How is php related to Question? Commented May 18, 2017 at 6:35

3 Answers 3

1

Append the <script> element to document without a src attribute. At a separate <script> element select script element and set data-amount attribute, and src attribute to path to resource.

    <script id="script"
        data-key="<?php echo $omise_public_key_test; ?>"
        data-image=" "
        data-frame-label="Acme"
        data-button-label="<?php echo translate('pay_now');?>"
        data-submit-label="<?php echo translate('submit');?>"
        data-location="no"
        data-amount=" "
        data-currency="us"
        >
     </script>  
     <script>           
     var total = $('#grand').html();
     $("#script[data-amount]")
     .attr({
       "data-amount": total,
       "src":"https://cdn.omise.co/omise.js.gz"
     });
     </script>
</form>
Sign up to request clarification or add additional context in comments.

Comments

0

Couple of things,

  1. If you want to manipulate the script tag, you can't do it from inside itself (as far as my experience has shown).

  2. You can't edit the script tag until it exists (is loaded).

  3. The button has the data-amount attribute. Can you not just edit that?

$(function(){
        // Button configure will merged and override with default configure.
        // And has effect with this button only.
        OmiseCard.configureButton('button[data-frame-label="Acme"]', {
          amount:           30.95,
          currency:         'thb',
          image:            'YOUR_LOGO_URL',
          frameLabel:       'Merchant name',
          frameDescription: 'Merchant description',
          submitLabel:      'Pay $30.95',
          buttonLabel:      'Pay with Omise',
          location:         'no',
          billingName:      '',
          billingAddress:   '',
          submitFormTarget: null,
        });
        OmiseCard.attach();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="cc-selector col-md-3">
    <form method="POST" action="<?php echo base_url() . 'home/cart_finish/go'; ?>" class="checkout-form">
        <input type="hidden" name="payment_type" value="omise">
        <script src="https://cdn.omise.co/omise.js.gz"
            data-key="Something"
            data-image=" "
            data-frame-label="Acme"
            data-button-label="Button"
            data-submit-label="Button"
            data-location="no"
            data-amount=" "
            data-currency="us"
            >
         </script>
    </form>

4 Comments

Sorry, it's not working. The attribute value needs to be set before the user clicks on the button.
I have updated the script to reflect your note above about what the omise script is doing
Still not working. It does set the value, but only after the button is rendered. I need it before.
From looking at the api on omise.co/omise-js-api#usage-sample the bottom of the page indicates a method for updating the configuration of a previously configured button. I have included all the value in the example in my snippet but you could likely just use the amount value to update the button as needed.
0

When the attribute is set, its value is always converted to a string. For example: element.dataset.example = null is converted into data-example="null".

HTML When the attribute is set, its value is always converted to a string. For example: element.dataset.example = null is converted into data-example="null".

HTML

<div id="partnerLocatorTagName">
    Carina Anand
</div>

JS

const partnerLocatorScript: HTMLScriptElement =
    this.dom.createElement('script');

partnerLocatorScript.id = 'partnerLocatorScriptID';

partnerLocatorScript.src =
    'https://google.com/prm/js/locator.js?v=4.15.3&pid=1234';

partnerLocatorScript.dataset.config =
    '{"directory_objects":{"directory_object":"","sublisting_object":""},"directoryObjects":{"directory_object":"","sublisting_object":""}}';
const partnerLocatorTagName = this.dom.getElementById(
    'partnerLocatorTagID'
);

partnerLocatorTagName?.appendChild(partnerLocatorScript);

FINAL

<div id="partnerLocatorTagName" src="https://google.com/prm/js/locator.js?v=4.15.3&pid=1234" data-config='{"directory_objects":{"directory_object":"","sublisting_object":""},"directoryObjects":{"directory_object":"","sublisting_object":""}}'>
        Carina Anand
    </div>

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.