Table's Inputs total depend on select option using jquery - javascript

I want the male female to be a separate total in the input box below. But at present both of them come in one input box, I don't know its mechanism. How can I do according to the design?
$(document).on("change", ".track", function() {
var sum = 0;
$(".track").each(function() {
sum += +$(this).val();
});
$("#total").val(sum);
});
<script src="https://code.jquery.com/jquery-3.5.1.min.js" crossorigin="anonymous"></script>
<table >
<tr>
<select name='type[]'>
<option value="male"> Male</option>
<option value="female"> Female</option>
</select>
<input type="text" name="amount[]" class="track" value="" /> <br>
</tr>
<tr>
<select name='type[]'>
<option value="female"> Female</option>
<option value="male"> Male</option>
</select>
<input type="text" name="amount[]" class="track" value="" /> <br>
</tr>
</table>
<hr>
<label> Male Total: </label>
<input type="text" id="total" value="" />
<label> Female Total: </label>
<input type="text" id="return" value="" />

Perhaps this example below can help you. It is without using jQuery (but if necessary, it is not difficult to rewrite using jQuery) and uses the form tag to catch a form change event:
const types = document.querySelectorAll('select[name="type[]"]');
const amounts = document.querySelectorAll('input[name="amount[]"]');
const totalMale = document.querySelector('#total-male');
const totalFemale = document.querySelector('#total-female');
const form = document.querySelector('#form');
const changeCallback = () => {
const total = {
male: 0,
female: 0
};
types.forEach((el, i) => {
total[el.value] += Number(amounts[i].value) || 0
});
totalMale.value = total.male;
totalFemale.value = total.female;
}
form.addEventListener('change', changeCallback);
changeCallback();
<form id="form">
<table>
<tr>
<select name='type[]'>
<option value="male"> Male</option>
<option value="female"> Female</option>
</select>
<input type="text" name="amount[]" class="track" value="" /> <br>
</tr>
<tr>
<select name='type[]'>
<option value="female"> Female</option>
<option value="male"> Male</option>
</select>
<input type="text" name="amount[]" class="track" value="" /> <br>
</tr>
</table>
</form>
<hr>
<label> Male Total: </label>
<input type="text" id="total-male" value="" />
<label> Female Total: </label>
<input type="text" id="total-female" value="" />
If the form tag bothers you, then you can do without it by marking all the elements of the form with one selector (for example, class .form-element):
const types = document.querySelectorAll('select[name="type[]"]');
const amounts = document.querySelectorAll('input[name="amount[]"]');
const totalMale = document.querySelector('#total-male');
const totalFemale = document.querySelector('#total-female');
const formElements = document.querySelectorAll('.form-element');
const changeCallback = () => {
const total = {
male: 0,
female: 0
};
types.forEach((el, i) => {
total[el.value] += Number(amounts[i].value) || 0
});
totalMale.value = total.male;
totalFemale.value = total.female;
}
formElements.forEach((el) => {
el.addEventListener('change', changeCallback)
});
changeCallback();
<table>
<tr>
<select name='type[]' class="form-element">
<option value="male"> Male</option>
<option value="female"> Female</option>
</select>
<input type="text" name="amount[]" class="track form-element" value="" /> <br>
</tr>
<tr>
<select name='type[]' class="form-element">
<option value="female"> Female</option>
<option value="male"> Male</option>
</select>
<input type="text" name="amount[]" class="track form-element" value="" /> <br>
</tr>
</table>
<hr>
<label> Male Total: </label>
<input type="text" id="total-male" value="" />
<label> Female Total: </label>
<input type="text" id="total-female" value="" />

Related

How to get values of input

I have a problem i dont know how can i read the value stored in <select><option> </option></select> and from radio buttons here i have my code with js and html
function inport(){
var name = document.getElementById("name").value;
var index = document.getElementById("index").value;
var phone = document.getElementById("phone").value;
var grade = document.getElementById("grade").value;
var session = document.getElementById("session").value;
console.log(name);
console.log(index);
console.log(phone);
console.log(grade);
console.log(session);
}
<form>
<div>
<h1>Details</h1>
<div>
<label>Name</label>
<input type="text" id="name">
</div>
<div>
<label>Index</label>
<input type="text" id="index">
</div>
<div>
<label>Phone</label>
<input type="text" id="phone">
</div>
<div id="grade">
<label><input type="radio" name="groupName" value="5"> 5</label>
<label><input type="radio" name="groupName" value="6"> 6</label>
<label><input type="radio" name="groupName" value="7"> 7</label>
<label><input type="radio" name="groupName" value="8"> 8</label>
<label><input type="radio" name="groupName" value="9"> 9</label>
<label><input type="radio" name="groupName" value="10"> 10</label>
<div>
</div>
<div>
<label>Session</label>
<select id="session">
<option value="checkbox">Decembar</option>
<option value="checkbox">November</option>
<option value="checkbox">Octomber</option>
<option value="checkbox">September</option>
<option value="checkbox">August</option>
<option value="checkbox">July</option>
<option value="checkbox">June</option>
<option value="checkbox">May</option>
<option value="checkbox">April</option>
<option value="checkbox">March</option>
<option value="checkbox">February</option>
<option value="checkbox">January</option>
</select>
</div>
<div>
<input type="button" value="Input student"id="import" onclick="inport();">
</div>
</div>
</form>
When i try to console.log my values of grade and session i want to get the number or month selected how can get the value should i change something in my html ? It works fine with first 3 because i just read from <input type="text"> but in other 2 i tried getting the value from radio button and select options. I think you can run this code snipped and see the output if you need more info let me know :).
Add name attribute in input.
JS Code
document.getElementById("show_radio").onclick = function () {
var radio = document.getElementsByName("gender");
var radio_selected;
for (var a = 0; a < radio.length; a++) {
if (radio[a].checked) {
radio_selected = radio[a].value;
}
}
document.getElementById("selected_radio").innerHTML = radio_selected;
}
<form>
<input type="radio" name="gender" value="Male" checked=""> Male<br>
<input type="radio" name="gender" value="Female"> Female<br>
<input type="radio" name="gender" value="I'd rather not inform"> I'd rather not inform
</form>
<button id="show_radio">Show</button>
<p id="selected_radio"></p>

How to reset checkbox values after user clicks new dropdown select option

I have a select box with a few dropdown options. Under each option there will be a checklist and total (each checklist item has a value). I need to accomplish this using pure javascript.
My problem is I don't know how to make each checklist specific to its dropdown item, and to reset the checkbox values after a user clicks a new dropdown option. I've tried calling the totalIt function in the display function but I can't get it to work.
Fiddle
My HTML is:
<div>
<select id="store">
<option value="A">Location A</option>
<option value="B">Location B</option>
<option value="C">Location C</option>
</select>
</div>
<div class="box A">
Location A<br>
<input name="product" value="20" type="checkbox" onclick="totalIt()" /> 20%<br>
<input name="product" value="15" type="checkbox" onclick="totalIt()" /> 15%<br>
Total:
<input value="0" readonly="readonly" type="text" name="total" />
</div>
<div class="box B">
Location B
<br>
<input name="product" value="20" type="checkbox" onclick="totalIt()" /> 20%<br>
<input name="product" value="15" type="checkbox" onclick="totalIt()" /> 15%<br>
Total:
<input value="0" readonly="readonly" type="text" name="total" />
</div>
<div class="box C">
Location C
<br>
<input name="product" value="20" type="checkbox" onclick="totalIt()" /> 20%<br>
<input name="product" value="15" type="checkbox" onclick="totalIt()" /> 15%<br>
Total:
<input value="0" readonly="readonly" type="text" name="total" />
</div>
CSS:
.hide {
display: none;
}
JS:
document.querySelector("select#store").addEventListener("change", () => {
display(event.target.value);
});
const boxs = document.querySelectorAll("div.box");
// Select box show/hide div
function display(value) {
for (const box of boxs) {
if (box.classList.contains(value)) {
box.classList.remove("hide");
} else {
box.classList.add("hide");
}
}
}
display("A");
// Total the values of the checkboxes
function totalIt() {
var input = document.getElementsByName("product");
var total = 0;
for (var i = 0; i < input.length; i++) {
if (input[i].checked) {
total += parseFloat(input[i].value);
}
}
document.getElementsByName("total")[0].value = total + "%";
}
Try to use the selected box children property. So that only those elements under the box would be updated. You are almost there I just had to tweak the code just a little.
HTML:
<div>
<select id="store">
<option value="A">Location A</option>
<option value="B">Location B</option>
<option value="C">Location C</option>
</select>
</div>
<div class="box A">
Location A <br>
Select your items:
<br>
<input name="proPercent1" value="20" type="checkbox" onclick="totalIt()"/> 20%<br>
<input name="proPercent2" value="15" type="checkbox" onclick="totalIt()"/> 15%<br>
Total:<br>
<input value="0" readonly="readonly" type="text" name="total" />
</div>
<div class="box B">
Location B <br>
Select your items:
<br>
<input name="proPercent1" value="20" type="checkbox" onclick="totalIt()"/> 20%<br>
<input name="proPercent2" value="15" type="checkbox" onclick="totalIt()"/> 15%<br>
Total:<br>
<input value="0" readonly="readonly" type="text" name="total" />
</div>
<div class="box C">
Location C <br>
Select your items:
<br>
<input name="proPercent1" value="20" type="checkbox" onclick="totalIt()" name="percentage1"/> 20%<br>
<input name="proPercent2" value="15" type="checkbox" onclick="totalIt()"/> 15%<br>
Total:<br>
<input value="0" readonly="readonly" type="text" name="total" />
</div>
JS:
document.querySelector("select#store").addEventListener("change", () => {
display(event.target.value);
});
const boxs = document.querySelectorAll("div.box");
let selectedBox = "";
function display(value) {
for (const box of boxs) {
if (box.classList.contains(value)) {
box.classList.remove("hide");
selectedBox = box;
} else {
box.classList.add("hide");
}
}
}
display("A");
// Total the values of the checkboxes
function totalIt() {
let total = 0;
if(selectedBox.children.proPercent1.checked) total = total + parseInt(selectedBox.children.proPercent1.value)
if(selectedBox.children.proPercent2.checked) total = total + parseInt(selectedBox.children.proPercent2.value)
selectedBox.children.total.value = `${total} %`;
}
Hopefully this helps
Hi,
I get solution to your problem...
document.querySelector("select#store").addEventListener("change", () => {
display(event.target.value);
});
const boxs = document.querySelectorAll("div.box");
// Select box show/hide div
function display(value) {
for (const box of boxs) {
if (box.classList.contains(value)) {
box.classList.remove("hide");
} else {
box.classList.add("hide");
}
}
}
display("A");
// Total the values of the checkboxes
function totalIt(Group) {
let input = document.querySelectorAll(`.${Group} [name=product]`),
total = 0,
totalInput = document.querySelector(`.${Group} [name=total]`)
input.forEach(input => {
if(input.checked) total += parseFloat(input.value);
totalInput.value = total + "%"
})
}
.hide {
display: none;
}
.box {
margin-top:30px;
}
<div>
<select id="store">
<option value="A">Location A</option>
<option value="B">Location B</option>
<option value="C">Location C</option>
</select>
</div>
<div class="box A">
Location A<br>
<input name="product" value="20" type="checkbox" onclick="totalIt('A')" /> 20%<br>
<input name="product" value="15" type="checkbox" onclick="totalIt('A')" /> 15%<br>
Total:
<input value="0" readonly="readonly" type="text" name="total" />
</div>
<div class="box B">
Location B
<br>
<input name="product" value="20" type="checkbox" onclick="totalIt('B')" /> 20%<br>
<input name="product" value="15" type="checkbox" onclick="totalIt('B')" /> 15%<br>
Total:
<input value="0" readonly="readonly" type="text" name="total" />
</div>
<div class="box C">
Location C
<br>
<input name="product" value="20" type="checkbox" onclick="totalIt('C')" /> 20%<br>
<input name="product" value="15" type="checkbox" onclick="totalIt('C')" /> 15%<br>
Total:
<input value="0" readonly="readonly" type="text" name="total" />
</div>
I Convert for loop to forEach array loop
I Change the selector from getElementsByNames to querySelector and querySelectorAll
I Add argument to the function represent the Group
I Noticed that you add class for each Group represent Name
I Use this to make input selector and totalInput selector dedicated for each group... Like document.querySelectorAll(.a [name=product])
Finally I call function and type name group in html
To uncheck a checkbox you set the element.checked value to false, (more here Check/Uncheck checkbox with JavaScript)
const allCheckboxes = document.querySelectorAll('input[type="checkbox"]');
function display (value) {
allCheckboxes.forEach(element => element.checked = false);
// your code
...
}

add CSS class on change event

I Have two Select Box with the same class.I am trying to add a class on the change of select option.
HTML:
<select class="selectProduct" name="product_id[]">
<option value="T-shirt">T-Shirt</option>
</select>
<input type="text" name="sku[]" placeholder="Code" id ="sku" class="code" />
<input type="text" name="qty[]" placeholder="Qty" id ="qty" class="qty" />
<select class="selectProduct" name="product_id[]" style="width:400px;">
<option value="shirt">Shirt</option>
</select>
<input type="text" name="sku[]" placeholder="Code" id ="sku" class="code" />
<input type="text" name="qty[]" placeholder="Qty" id ="qty" class="qty" />
jQuery:
$(document).on('change', '.selectProduct', function(e) {
var idSize = $(this).val();
$(this).find(".code").addClass(idSize);
});
Your select and input is in same hierarchy so use next to get input and add class .
find search inside the element you give so why your code is fail.
$(document).on('change', '.selectProduct', function(e) {
var idSize = $(this).val();
$(this).next(".code").addClass(idSize);
$(this).next().next(".qty").addClass(idSize);
});
.shirt {
outline: none !important;
border: 1px solid red;
}
.T-shirt {
outline: none !important;
border: 1px solid blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="selectProduct" name="product_id[]">
<option value="T-shirt">T-Shirt</option>
<option value="shirt">shirt</option>
</select>
<input type="text" name="sku[]" placeholder="Code" id="sku" class="code" />
<input type="text" name="qty[]" placeholder="Qty" id="qty" class="qty" />
<select class="selectProduct" name="product_id[]" style="width:400px;">
<option value="T-shirt">T-Shirt</option>
<option value="shirt">Shirt</option>
</select>
<input type="text" name="sku[]" placeholder="Code" id="sku" class="code" />
<input type="text" name="qty[]" placeholder="Qty" id="qty" class="qty" />
The user can only change the value if the select box has more than one option, and it's easier to find the correct .code input to modify if we group the related inputs together.
Here's a revised version of your code that does as you suggest:
const container = document.querySelector('#container-div');
container.addEventListener('change', addValAsClass);
function addValAsClass(event){
const codeBox = event.target.closest(".input-group").querySelector(".code");
codeBox.classList.add(event.target.value);
console.log(`classList: ${codeBox.getAttribute('class')}`);
}
div + div { margin-top: 10px; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="container-div">
<div class="input-group">
<select class="selectProduct">
<option value="opt1">Opt 1</option>
<option value="opt2">Opt 2</option>
</select>
<input placeholder="Code" class="code" />
<input placeholder="Qty" class="qty" />
</div>
<div class="input-group">
<select class="selectProduct">
<option value="optA">Opt A</option>
<option value="optB">Opt B</option>
</select>
<input placeholder="Code" class="code" />
<input placeholder="Qty" class="qty" />
</div>
</div>
i suggest wrapping the select + input into another div, to make finding easier.
your code might not work, because you have elements with non-unique id (sku and qty).
<div> <!-- wrapper start -->
<select class="selectProduct" name="product_id[]">
<option value="T-shirt">T-Shirt</option>
</select>
<input type="text" name="sku[]" placeholder="Code" id="code" class="code" />
<input type="text" name="qty[]" placeholder="Qty" id="qty" class="qty" />
<div> <!-- wrapper end -->
<div> <!-- wrapper start -->
<select class="selectProduct" name="product_id[]">
<option value="T-shirt">T-Shirt</option>
</select>
<input type="text" name="sku[]" placeholder="Code" id="code" class="code" />
<input type="text" name="qty[]" placeholder="Qty" id="qty" class="qty" />
<div> <!-- wrapper end -->
now you can find the next input by class in its parent container:
$(document).on('change', '.selectProduct', function(e) {
var idSize = $(this).val();
$(this).parent().find(".code").addClass(idSize); // <-- parent
})

Adding and deleting input fields on click of a button in a form in HTML

Please run the below code.
i am a beginner in coding.
I am making a form which contains various fields.
As you can see that in the Education field on click of add education button I am successfully able to add another education field but I am not able to delete the exact field. Please help!
Thanks in advance
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Assignment</title>
</head>
<body>
<h1>Candidate Information</h1>
<h2>Submit Your Info</h2>
<form>
<!-- name -->
<label for="name"><b>Name</b></label>
<input type="text" placeholder="Enter your name" name="name" required><br />
<!-- gender -->
<br/><label for="gender"><b>Gender</b></label><br/>
<br/><label for="male"> Male</label>
<input type="radio" id="male" name="gender" value="male" />
<label for="female"> Female</label>
<input type="radio" id="female" name="gender" value="female" /><br />
<!-- address -->
<br /><label for="address"><b>Address</b></label>
<br /><textarea rows="6" name="address" cols="70" placeholder="Enter your address" maxlength="200" required></textarea><br />
<!-- email -->
<br/><label for="email"><b>Email</b></label>
<input type="email" placeholder="Enter your email" name="email" pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,}$" title="Please enter the correct email" required><br />
<!-- phone -->
<br/><label for="phone"><b>Phone</b></label>
<input type="tel" placeholder="Enter your phone number" name="phone" pattern="[0-9]{10}" title="Phone number can only be 10 digits" required><br /><br />
<!-- education -->
<p><b>Education</b></p>
<div id="education">
<label for="level"><b>Level:</b></label>
<select name="level" id="level">
<option>-Please select a level-</option>
<option value="SSC">SSC</option>
<option value="HSSC">HSSC</option>
<option value="Diploma">Diploma</option>
<option value="BE">BE</option>
<option value="BTech">BTech</option>
<option value="BCA">BCA</option>
<option value="MCA">MCA</option>
</select>
<label for="year"><b>Year</b></label>
<input type="number" name="year1" max="2020" min="2000" />
<label for="grade"><b>Grade/%</b></label>
<input type="text" name="grade1" />
</div>
<input name="addeducation" type="button" value="Add Education" onClick="addEducation('education');"><br/><br/>
<script>
var counter = 1;
var limit = 4;
function addEducation(divname) {
if (counter == limit) {
alert("You have reached the limit of adding " + counter + " inputs");
}
else {
var newdiv = document.createElement('div');
newdiv.className='addeduclass'+counter;
newdiv.innerHTML = "<label><b>Level: </b><label/>" + "<select><option>-Please select a level-</option><option value='SSC'>SSC</option><option value='HSSC'>HSSC</option><option value='Diploma'>Diploma</option><option value='BE'>BE</option><option value='BTech'>BTech</option><option value='BCA'>BCA</option><option value='MCA'>MCA</option></select>" +
"<label><b> Year</b><label/> " + "<input type='number' name='year' max='2020' min='2000'/>" +
"<label><b>Grade/%</b><label/> " + "<input type='text' name='grade'/>"+
"<button type='button' onClick='removeEducation('education',newdiv.className)'>Remove</button>";
document.getElementById(divname).appendChild(newdiv);
counter++;
console.log(counter);
console.log(newdiv.className);
}
}
function removeEducation(edudiv,divname) {
console.log("deleted");
document.getElementsByClassName(edudiv).removeChild(divname);
counter--;
}
</script>
<!-- skills -->
<p><b>Skills</b></p>
<div id="skills">
<label for="skillname"><b>Name</b></label>
<input type="text" name="skillname">
<label for="rating"><b>Rating:</b></label>
<select name="rating" id="rating">
<option>-Please select a rating-</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
<input name="addskill" type="button" value="Add Skill" onClick="addSkill('skills');"><br/>
<script>
var skillcounter = 1;
var skilllimit = 3;
function addSkill(divname1) {
if (skillcounter == skilllimit) {
alert("You have reached the limit of adding " + skillcounter + " inputs");
}
else {
var snewdiv = document.createElement('div');
snewdiv.className="addskillclass"
snewdiv.innerHTML = "<label><b>Name</b></label>" + "<input type='text' name='skillname1'/>"+
"<label><b>Rating:</b></label>" + "<select><option>-Please select a rating-</option><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option><option value='10'>10</option></select>";
document.getElementById(divname1).append(snewdiv);
skillcounter++;
console.log(counter);
}
}
// function removeSkill(divname1) {
// }
</script>
<!-- hobby -->
<br/>
<label for="hobby"><b>Hobby</b></label>
<input type="text" placeholder="Enter your hobby" name="hobby"><br /><br />
<!-- photourl -->
<label for="photourl"><b>Photo</b></label>
<input type="url" name="photourl" placeholder="photo url" /><br />
<!-- submit -->
<input type="submit" value="Submit" />
</form>
</body>
</html>
You could simplify this a little like so:
Pass the delete button into the function removeEducation using the keyword this, so change to the following on your delete education buttons:
onClick='removeEducation(this);'
Then in your removeEduction function, you pass in the delete button which is clicked, traverse it to the parent node of the parent div of the button and then use removeChild to remove the parent div of the delete button:
function removeEducation(deleteButton) {
deleteButton.parentNode.parentNode.removeChild(deleteButton.parentNode);
counter--;
}
Just a small piece of advice, it is recommended to add event handling to elements via javascript using .addEventListener instead of inline in the html You can read more about that here (https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Assignment</title>
</head>
<body>
<h1>Candidate Information</h1>
<h2>Submit Your Info</h2>
<form>
<!-- name -->
<label for="name"><b>Name</b></label>
<input type="text" placeholder="Enter your name" name="name" required><br />
<!-- gender -->
<br/><label for="gender"><b>Gender</b></label><br/>
<br/><label for="male"> Male</label>
<input type="radio" id="male" name="gender" value="male" />
<label for="female"> Female</label>
<input type="radio" id="female" name="gender" value="female" /><br />
<!-- address -->
<br /><label for="address"><b>Address</b></label>
<br /><textarea rows="6" name="address" cols="70" placeholder="Enter your address" maxlength="200" required></textarea><br />
<!-- email -->
<br/><label for="email"><b>Email</b></label>
<input type="email" placeholder="Enter your email" name="email" pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,}$" title="Please enter the correct email" required><br />
<!-- phone -->
<br/><label for="phone"><b>Phone</b></label>
<input type="tel" placeholder="Enter your phone number" name="phone" pattern="[0-9]{10}" title="Phone number can only be 10 digits" required><br /><br />
<!-- education -->
<p><b>Education</b></p>
<div id="education">
<label for="level"><b>Level:</b></label>
<select name="level" id="level">
<option>-Please select a level-</option>
<option value="SSC">SSC</option>
<option value="HSSC">HSSC</option>
<option value="Diploma">Diploma</option>
<option value="BE">BE</option>
<option value="BTech">BTech</option>
<option value="BCA">BCA</option>
<option value="MCA">MCA</option>
</select>
<label for="year"><b>Year</b></label>
<input type="number" name="year1" max="2020" min="2000" />
<label for="grade"><b>Grade/%</b></label>
<input type="text" name="grade1" />
</div>
<input name="addeducation" type="button" value="Add Education" onClick="addEducation('education');"><br/><br/>
<script>
var counter = 1;
var limit = 4;
function addEducation(divname) {
if (counter == limit) {
alert("You have reached the limit of adding " + counter + " inputs");
}
else {
var newdiv = document.createElement('div');
newdiv.className='addeduclass'+counter;
newdiv.innerHTML = `<label><b>Level: </b><label/>
<select>
<option>-Please select a level-</option>
<option value='SSC'>SSC</option>
<option value='HSSC'>HSSC</option>
<option value='Diploma'>Diploma</option>
<option value='BE'>BE</option>
<option value='BTech'>BTech</option>
<option value='BCA'>BCA</option>
<option value='MCA'>MCA</option>
</select>
<label><b> Year</b><label/> <input type='number' name='year' max='2020' min='2000'/>
<label><b>Grade/%</b><label/> <input type='text' name='grade'/>
<button type='button' onClick="removeEducation('education','${newdiv.className}')">Remove</button>`;
document.getElementById(divname).appendChild(newdiv);
counter++;
console.log(counter);
console.log(newdiv.className);
}
}
function removeEducation(edudiv,divname) {
console.log("deleted");
document.getElementById(edudiv).removeChild(document.getElementsByClassName(divname)[0]);
counter--;
}
</script>
<!-- skills -->
<p><b>Skills</b></p>
<div id="skills">
<label for="skillname"><b>Name</b></label>
<input type="text" name="skillname">
<label for="rating"><b>Rating:</b></label>
<select name="rating" id="rating">
<option>-Please select a rating-</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
<input name="addskill" type="button" value="Add Skill" onClick="addSkill('skills');"><br/>
<script>
var skillcounter = 1;
var skilllimit = 3;
function addSkill(divname1) {
if (skillcounter == skilllimit) {
alert("You have reached the limit of adding " + skillcounter + " inputs");
}
else {
var snewdiv = document.createElement('div');
snewdiv.className="addskillclass"
snewdiv.innerHTML = "<label><b>Name</b></label>" + "<input type='text' name='skillname1'/>"+
"<label><b>Rating:</b></label>" + "<select><option>-Please select a rating-</option><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option><option value='10'>10</option></select>";
document.getElementById(divname1).append(snewdiv);
skillcounter++;
console.log(counter);
}
}
// function removeSkill(divname1) {
// }
</script>
<!-- hobby -->
<br/>
<label for="hobby"><b>Hobby</b></label>
<input type="text" placeholder="Enter your hobby" name="hobby"><br /><br />
<!-- photourl -->
<label for="photourl"><b>Photo</b></label>
<input type="url" name="photourl" placeholder="photo url" /><br />
<!-- submit -->
<input type="submit" value="Submit" />
</form>
</body>
</html>
Try this out. It worked. Made few changes to the remove function

Form multiplication

I need these two fields to be multiplied together based on selected form options.
Okay so it works without all the extra fields but once i added the rest of the form the calculation stopped working. :(
<form action="mailer.php" data-validate="parsley" method="post" >
<p><strong>Full Name<span class="red">*</span></strong></p>
<input name="cf_name" data-required="true" class="send" type="text" />
<p><strong>Email Address<span class="red">*</span></strong></p>
<input name="cf_email" data-required="true" data-type="email" class="send" type="text" />
<p><strong>Cellphone No.<span class="red">*</span></strong></p>
<input name="cf_cell" data-required="true" class="send" type="text" />
<p><strong>Instrument Type<span class="red">*</span></strong></p>
<select name="cf_instrument" size="1" class="option" >
<option value="Piano">Piano</option>
<option value="Vocals">Vocals</option>
<option value="Guitar">Guitar</option>
<option value="Bass">Bass</option>
<option value="Flute">Flute</option></select>
<p><strong>Lesson Type<span class="red">*</span></strong></p>
<select name="cf_package_type" id="cf_package_type" size="1" class="option">
<option value="100">Beginner Lesson - R100</option>
<option value="130">Advanced Lesson - R130</option>
<option value="160">Professional Lesson - R160</option></select>
<p><strong>No. of Lessons<span class="red">*</span></strong></p>
<select id="number-of-lessons" name="cf_number" size="1" class="option" onchange='test()'>
<option name="1" value="1" selected="selected">1</option>
<option name="2" value="2">2</option>
<option name="3" value="3">3</option>
<option name="4" value="4">4</option></select>
<script src="js/datepair.js"></script>
<p><strong>Lesson Date & Time<span class="red">*</span></strong></p>
<p class="datepair" data-language="javascript">
<input type="text" name="cf_booking_date" class="date start" data-required="true" />
<input type="text" name="cf_start_time" class="time start" data-required="true" /> to
<input type="text" name="cf_end_time" class="time end" data-required="true" /></p>
<script src="js/datepair.js"></script>
<p id="lesson-2" class="datepair" data-language="javascript">
<input type="text" name="cf_booking_date" class="date start" data-required="true" />
<input type="text" name="cf_start_time" class="time start" data-required="true" /> to
<input type="text" name="cf_end_time" class="time end" data-required="true" /></p>
<script src="js/datepair.js"></script>
<p id="lesson-3" class="datepair" data-language="javascript">
<input type="text" name="cf_booking_date" class="date start" data-required="true" />
<input type="text" name="cf_start_time" class="time start" data-required="true" /> to
<input type="text" name="cf_end_time" class="time end" data-required="true" /></p>
<script src="js/datepair.js"></script>
<p id="lesson-4" class="datepair" data-language="javascript">
<input type="text" name="cf_booking_date" class="date start" data-required="true" />
<input type="text" name="cf_start_time" class="time start" data-required="true" /> to
<input type="text" name="cf_end_time" class="time end" data-required="true" /></p>
<!-- HIDDEN FIELD - HONEYPOT ANTI_SPAM -->
<input id="website" class="using" name="website" type="text" />
<!-- END -->
<input name="Submit" class="submit" value="Book Now" type="submit" /></form>
<input type="text" value="100" disabled="disabled" id="result">
Corrected JS used:
<script type="text/javascript">
$("select").change(function(){
var val1 = + ($("#cf_package_type").val());
var val2 = + ($("#number-of-lessons").val());
$("#result").val(val1*val2);
});
</script>
I know i'm probably very off-track but if someone could please help me with this it would be a life-saver!
Here's the JS fiddle to help you - http://jsfiddle.net/GuBPL/10/
Thank you.
Try with following code:
<p><strong>Lesson Type<span class="red">*</span></strong></p>
<select id="cf_package_type" name="cf_package_type" size="1" class="option">
<option value="100">Beginner Lesson - R100</option>
<option value="130">Advanced Lesson - R130</option>
<option value="160">Professional Lesson - R160</option>
</select>
<p><strong>No. of Lessons<span class="red">*</span></strong></p>
<select id="number-of-lessons" name="cf_number" size="1" class="option">
<option value="1" selected="selected">1</option>
<option name="2" value="2">2</option>
<option name="3" value="3">3</option>
<option name="4" value="4">4</option>
</select>
<input type="text" disabled="disabled" id="result">
And JS:
$(document).ready(function(){
$("#number-of-lessons").change(function(){
var val1 = parseInt($("#cf_package_type").val());
var val2 = parseInt($(this).val());
$("#result").val(val1*val2);
});
});
What's changed ?
First select got id="cf_package_type" attribute, which is used to get its value with:
$("#cf_package_type").val()
Removed onchange='test()' because we do it with jQuery:
$("#number-of-lessons").change()
Get value of second select with:
$(this).val()
because we work on it already.
parseInt used just to be sure, we use integers, not strings.
Edit:
To calculate result on page load, use:
$(document).ready(function(){
var calculate = function(){
var val1 = parseInt($("#cf_package_type").val());
var val2 = parseInt($('#number-of-lessons').val());
$("#result").val(val1*val2);
};
$("#number-of-lessons").change(calculate);
calculate();
});
You use $('.select') while the elements have class name option,
You should use change event instead of keyup
you use $('.cf_package_type') while it's a name, not class attribute
http://jsfiddle.net/GuBPL/4/
$(document).ready(function(){
$(".option").change(function(){
var val1 = +$("[name=cf_package_type]").val();
var val2 = +$("[name=cf_number]").val();
$("#result").val(val1*val2);
});
});

Categories