sharingsunshine
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
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,
Here is the code
<script>
document.getElementById("order_comments").innerHTML = "I want membership";
</script>
<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 <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>
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,
Hi,
You could add a custom class as suggested in the other comment and put the CSS attributes you want inside it :
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 :
If you want to toggle the style on click you could use " toggle()" instead :
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*/
}
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');
});
});
If you want to toggle the style on click you could use " toggle()" instead :
this.classlist.toggle('my-class');
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"?
ASKER
color of the button
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.