1

I tried to add dynamically a form in JavaScript, but when I click on the submit button nothing happens. I know that is because the button has no listener, but I don't know what I have to add in the listener for a normal sending button behavior.

Here some of my code

document.getElementById("partTwo").innerHTML += "<form id='download_report' method='post' class='form-horizontal' role='form' action='include/client.process.php'>";
document.getElementById("partTwo").innerHTML += "<input type='hidden' name='jobid' value='" + jobId + "'/>";
document.getElementById("partTwo").innerHTML += "<input type='hidden' name='job' value='" + job_details.heavyInfosJson + "'/>";
document.getElementById("partTwo").innerHTML += "<input type='hidden' name='download_report' value='1'/>";
document.getElementById("partTwo").innerHTML += "<button id='download_report_button' type='submit' class='btn btn-default btn-xs'>T&eacute;l&eacute;charger le rapport</button>";
document.getElementById("partTwo").innerHTML += "</form>";

I want from the listener to juste send the form to my client.process.php

I know that it s a beginner question but any help would be appreciated. Thanks

0

3 Answers 3

2

Working fiddle.

You should add the inputs inside form and not inside div partTwo, so your code should be like :

document.getElementById("partTwo").innerHTML += "<form id='download_report' method='post' class='form-horizontal' role='form' action='include/client.process.php'>";
document.getElementById("download_report").innerHTML += "<input type='hidden' name='jobid' value='" + jobId + "'/>";
document.getElementById("download_report").innerHTML += "<input type='hidden' name='job' value='" + job_details.heavyInfosJson + "'/>";
document.getElementById("download_report").innerHTML += "<input type='hidden' name='download_report' value='1'/>";
document.getElementById("download_report").innerHTML += "<button id='download_report_button' type='submit' class='btn btn-default btn-xs'>T&eacute;l&eacute;charger le rapport</button>";

Hope this helps.

Sign up to request clarification or add additional context in comments.

5 Comments

Ok, i understand but are you sure for the last element ? it would be document.getElementById("partTwo").innerHTML += "</form>"; no ?
No champ, the form is autoclosed.
So if I underwent well, i don't need to add listener on button ? The button behavior is correct even if it is created dynamically ?
Yes that true the type='submit' in button tell him that it should submit the parent form.
Be advised button with type sybmit will cause page reloading. if you dont want page reloadint - like in SPA, you need use button listeners.
1

The problem is that <form> tag is autoclosed. innerHTML returns well formed html. See this question

2 Comments

That a point, but actually the problem is appending the whole tags in the same level when the inputs should be inside the form, check my answer please.
@Zakaria Acharki i agree with you. I have provided cause and you have provided solution. Perfect job! :)
0

You should consider using createElement rather than innerHTML, see 2946656

Here is a basic example of how to create a form and append it to the page:

var form = document.createElement("form");
form.setAttribute('action', 'contact.php');
form.setAttribute('method', 'POST');

var inputField = document.createElement("input");
inputField.setAttribute('value', 'example');
form.appendChild(inputField);

document.body.appendChild(form);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.