I am trying to create a simple html file containing the monthly salary of employees and must calculate the tax payable for each salary!
I use an “add” button to add more employee textfield which creates a new class of div containing the similar div!
On onchange event of the salary textfield I can not set the tax value of the appropriate employee!
Unfortunately I can not upload the screenshot so I put all code to make my question as clear as possible!
This is the jquery code for adding employee area:
$(document).ready(function(){
var counter = 2;
$("#add_emp").click(function () {
var newTextBoxDiv = $(document.createElement('div'))
.attr("class", 'em_area' + counter);
newTextBoxDiv.after().html('<br><label>Employee '+counter+' </label> <input id=em_name name=em_name type=text required /> <label>Position</label> <input id=em_post name=em_post type=text required /> <label>Monthly Salary</label> <input id=em_sal name=em_sal size=10 type=text required />       <label>Salary Tax</label> <input id=em_taxds type=text size=10 required /> ' );
newTextBoxDiv.appendTo("#all_emps");
counter++;
});
$("#del_emp").click(function () {
counter--;
$(".em_area" + counter).remove();
});
});
This is the html code:
<div id="all_emps">
<div class="em_area">
<label>Employee 1</label>
<input type=text required />
<label>Position</label>
<input type=text required />
<label>Monthly Salary</label>
<input id=em_sal type=text required size=10 onchange="calculateTax(this.value);" />
     
<label>Salary Tax</label>
<input id=em_tax name=em_tax type=text required size=10 readonly />
               
<a href="#" id="add_emp"><img src="add.jpg" width="30" height="30"></img></a>
         
<a href="#" id="del_emp"><img src="minus.jpg" width="30" height="30"></a>
</div>
</div>
This is the tax calculation code in which I have problem:(??????)
function calculateTax(sal)
{
int salary = parseInt(sal);
if(salary < 5000)
{
document.getElementById("em_tax").value = '0' ;
}
else if(salary>5000 || salary=5000)
{
var due_tax ;
due_tax= salary/100;
due_tax=salary - due_tax;
document.getElementById("????????").value = due_tax;
}
}
The first thing I observed is that you have duplicate Id’s in your HTML.. Make sure the id is unique on the page.. Try Giving them class Names instead..
Writing Up the change event in JQuery
Check FIDDLE