Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to hide a button with multiple forms in page?

I have an e-commerce page, in the page I have multiple forms each with a different name and id but every button has the same id and name., e.g;

<form method="post" name="form12345" id="form12345" action="..."> 
    <input type="button" class="btn-BuyOff" value="Acquista" id="addtocart" name="addtocart">
</form>

How can I modify the visibility of the button in a specific form using the instruction in Javascript:

document.getElementById('addtocart').style.visibility = 'hidden';

the code above modify only the first button in the page, I need to address a specific button in the page.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Note: I can not modify the id or name of the buttons because the original code is automatically generate by the e-commerce.

Example:

<form name="form1" id="form1">
    form content
    <input type="button" name="addtocart" id="addtocart">
</form>
<form name="form2" id="form2">
    form content
    <input type="button" name="addtocart" id="addtocart">
</form>
<form name="form3" id="form3">
    form content
    <input type="button" name="addtocart" id="addtocart"> // this is the button to hide
</form>
<form name="form4" id="form4">
    form content
    <input type="button" name="addtocart" id="addtocart">
</form>

>Solution :

element’s name are hierarchical dependent in each form:

so, ther is no real interest to use id attributes in a form.

const formName = 'form3';

document.forms[formName].addtocart.style.visibility = 'hidden';


// document.forms.form3.addtocart.style.visibility = 'hidden';
form {
 height     : 2em;
 background : lightgreen;
 margin     : 0.5em;
 }
<form name="form1" >
    form content
    <input type="button" name="addtocart" >
</form>
<form name="form2" >
    form content
    <input type="button" name="addtocart" >
</form>
<form name="form3" >
    form content
    <input type="button" name="addtocart" > // this is the button to hide
</form>
<form name="form4" >
    form content
    <input type="button" name="addtocart" >
</form>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading