Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7908697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:04:07+00:00 2026-06-03T12:04:07+00:00

I want to write hidden inputs to the form based on which checkboxes are

  • 0

I want to write hidden inputs to the form based on which checkboxes are selected. In my example im not getting errors but it still wont pass the hidden fields-(edited)

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<div id="test"></div>
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="youremail@mail.com">
<input type="hidden" name="currency_code" value="US">
<input type="checkbox" id="chkbox1" name="camp1" value="a">
<input type="checkbox" id="chkbox2" name="camp2" value="b">
<input type="submit" value="PayPal">
</form>
<script type="text/javascript">
var element = document.getElementById('test');
if(element != null && element.checked) {

var newElement = document.createElement('input');
newElement.type = "hidden";
newElement.name = "item_name_1";
newElement.value = "MyItem #1";

document.getElementById('chkbox1').appendChild(newElement);

var newElement2 = document.createElement('input');
newElement2.type = "hidden";
newElement2.name = "amount_1";
newElement2.value = "7.00";

document.getElementById('test').appendChild(newElement2);
}

Theoretically this should work, but it’s got me stumped. If anyone can offer some advice it would be much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T12:04:09+00:00Added an answer on June 3, 2026 at 12:04 pm

    First, never use document.write while in the context of a webpage. That’s bad practice. If you want to dynamically add an element with pure javascript, you would simply create a new element:

    var newElement = document.createElement('input');
    newElement.type = "hidden";
    newElement.name = "something";
    newElement.value = "someValue";
    
    document.getElemenytById('somecontainer').appendChild(newElement);
    

    Secondly, the error means that the document.getElementById call for one of your statements did not find the specified element. You need to be null checking the results:

    var element = document.getElementById('chkbox1');
    
    if(element != null && element.checked)
       // Do Stuff
    

    Now, without knowing the placement of where your script is, and where this form is in your page, you could suffering from the loading placement. If your script runs before the HTML is loaded, you wont find the elements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form that does not seem to want to write its data
I want write a little code analyzer which parses nested structures and translates into
I want to write a syntax highlighting extension for Emacs, but I Googling of
I have a form and I want it to not be displayed on submit
I want to write a program which would download all post offices given a
Hello I want write my own desktop sharing application in Java. The application should
I want to write an NSOutputStream to a server with apple's sample code: NSURL
I want to write a stored procedure to increment the value of an int
I want to write a unit test for a Django manage.py command that does
I want to write a JAR file that makes use of the javax servlet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.