Button Onclick Function With Document.getElementById
I have created a button using woocommerce for each product, when clicked on it is meant to dropdown a form with its corresponding ID. I'm using woocommerce for displaying the products, and gravity forms for the forms.
I have placed this code html code to create the function for the button :
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">SPECIFY PRODUCT</button>
<div id="gform_<?php echo get_the_ID(); ?>" class="dropdown-content">
<?php gravity_form( 1, false, false, false, '', true ); ?>
</div>
</div>
I also have the script above this html block (as i tried putting in the head section but was giving me a 'null is not an object error' for the document.GetElem...)
<script>
var theid = "<?php echo get_the_ID(); ?>";
function myFunction() {
document.getElementById('gform_' + theid).classList.toggle('show');
}
</script>
When i go to click on either of the 3 buttons it only drops down on the 3rd (pulling that ID first and not getting the others).
How can I make all the buttons dropdown and not just one?
Any help would be greatly appreciated as I've been trying to work this out for a while now..
Here is the link: http://www.ctagroup.com.au/cta-group-home/products/tactile-guidance/suresteel/suresteel-classic/
Answer
try the code below:
<script>
function myFunction(myid) {
document.getElementById('gform_' + myid).classList.toggle('show');
}
</script>
<div class="dropdown">
<button onclick="myFunction(<?php echo get_the_ID(); ?>)" class="dropbtn">SPECIFY PRODUCT</button>
<div id="gform_<?php echo get_the_ID(); ?>" class="dropdown-content">
<?php gravity_form( 1, false, false, false, '', true ); ?>
</div>
</div>
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM