I'm trying to create a voting system. Whenever a user fills out a form with the title and two possible options you can vote on, the data is appended to a div element on the page using ajax. Using radio buttons, when the user clicks on one of the two possible voting options (one input has value of 0, the other has value of 1), I want to send the value (either 0 or 1) to a php file, which will then be inserted into a database.
Here's what's happening when I click on the input field: https://jsfiddle.net/crduling/dczup26h/36 HTML:
<div class='display-polls-container'>
</div>
<button id='user-created-poll'>Click for new poll</button>
Javascript:
$("#user-created-poll").click(function() {
$(".display-polls-container").append("<div class='poll-container'><form><div class='title'><p class='user'>Username</p><p class='title-text'>Poll title</p></div><label class='userPollLabel'><div class='inner-content'><input type='radio' name='polls' value='0'>First option</div></label><label class='userPollLabel'><div class='inner-content'><input type='radio' name='polls' value='1'>Second item</div></label></form></div>");
$("input:radio").click(function() {
var value = $(this).val();
alert(value);
});
});
//How do I make it so that when I click on one of the inputs, it only gives the value of the input that I'm clicking and not the value of all the inputs?
CSS:
html,
body {
margin: 0;
padding: 0;
height: 100%;
}
button {
position: absolute;
top: 0;
right: 2%;
width: 25%;
height: 10%;
}
.poll-container {
border-style: solid;
}
What I want to happen is for only one value to be returned, but when there are multiple polls, the value 0 or 1 returns multiple times.