Comment faire un sondage avec javascript ?
1 Answers
There are several ways to make a survey with JavaScript, depending on your specific requirements and the tools you want to use. Here is a basic example of how to create a survey form using JavaScript:
Create an HTML form with the questions you want to ask in your survey. For example:
<form>
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br>
<label for="age">Age:</label>
<input type="number" id="age" name="age"><br>
<label for="gender">Gender:</label>
<select id="gender" name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">Other</option>
</select><br>
<label for="feedback">Feedback:</label>
<textarea id="feedback" name="feedback"></textarea><br>
<button type="submit">Submit</button>
</form>
Add an event listener to the submit button to handle the form submission:
const form = document.querySelector('form');
form.addEventListener('submit', (event) => {
event.preventDefault();
const formData = new FormData(form);
const formObject = Object.fromEntries(formData.entries());
// Do something with the form data, e.g. send it to a server
});
This code listens for the submit event on the form, prevents the default form submission behavior, and converts the form data into a JavaScript object using the FormData constructor and the Object.fromEntries method.
Handle the form data as desired, for example by sending it to a server using an HTTP request library like axios or fetch.
1
Likes
0
Dislikes
Likes
Dislikes
0
Comments
Login to add comment