Link to home
Start Free TrialLog in
Avatar of sharingsunshine
sharingsunshineFlag for United States of America

asked on

Need Existing Javascript Button To Change OnClick

I have a javascript button at https://www.theherbsplace.com/rebate-program/ it is found at the bottom of the page.

Here is the code
<script>
document.getElementById("order_comments").innerHTML = "I want membership";
</script>

Open in new window


<p><a class="button exclusive biz-op-button" title="I Want Membership!" href="#" data-register="true">Get My Membership</a></p>
<p class="form-row notes" id="order_comments" data-priority="">
    <label for="order_comments" class="">Order notes&nbsp;<span class="optional">(optional)</span></label>
    <span class="woocommerce-input-wrapper">
        <textarea name="order_comments" class="input-text " id="order_comments" placeholder="Notes about your order, e.g. special notes for delivery." rows="2" cols="5"></textarea>
    </span>
</p>

Open in new window


The button works fine but I need it to change colors and text on click.  This is so the customer will know their request has been logged.

I am not very strong in javascript.  I see on the net some examples but I don't know enough to fold them into what I already have working.

Thanks,
Avatar of hielo
hielo
Flag of Wallis and Futuna image

You can add an inline onclick event handler that adds an CSS class ('clicked-button'), and then you control the aesthetics through css:
<style type="text/css">
.clicked-button{
  background-color:red;
  color:white;
}
</style>

<p><a class="button exclusive biz-op-button" title="I Want Membership!" href="#" data-register="true" 
onclick="this.className = this.className.replace(' clicked-button','') + ' clicked-button';">Get My Membership</a></p>

Open in new window


You can also give your link/button an id and attach an event handler upon page load instead of doing it inline.
Hi,

You could add a custom class as suggested in the other comment and put the CSS attributes you want inside it :

.my-class{
   /*Your attributes here*/
}

Open in new window


I will suggest the use of addEventListener() to attach the click event and to really prevent the use of the old "inline-events", you could attach the event inside the load event to make sure that the element will be found after the full DOM is loaded and the event will be attached successfully :

document.addEventListener('DOMContentLoaded', function(){
    document.querySelector('.biz-op-button').addEventListener('click', function(){
        document.getElementById("order_comments").classlist.add('my-class');
    });
});

Open in new window


If you want to toggle the style on click you could use " toggle()" instead :

this.classlist.toggle('my-class');

Open in new window

The button works fine but I need it to change colors and text on click

Do you want to change the color of the button or the color of the paragraph "order_comments"?
Avatar of sharingsunshine

ASKER

color of the button
Zakaria, I am new to javascript so looking at your example I am not clear how to implement it into my website.  For example, I am not clear where I would put in the button verbiage.  Please put all of the code in your example.
ASKER CERTIFIED SOLUTION
Avatar of sharingsunshine
sharingsunshine
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial