sshivanna
asked on
Add one to many table rows based on checkbox values
I have one select list and 4 checkboxes. I have enter the option value and the value of the checkbox as 2 separate values in a row.
If one checkbox is checked, I am able to add a table row. I need help in adding multiple rows at one time if multiple checkboxes are checked.
Please help.
Thank you
sshivanna
So far this is my code...
SCRIPT
<script type="text/javascript">
$(function() {
$("#btn_addrole").click(fu nction(){
if ($("#Launcher").is(":check ed") == true) {
$("#rolestable > tbody:last").append('<tr>< td>'+$("#s urveyoptio ns option:selected").val()+'< /td><td>'+ $("#Launch er").val() +'</td></t r>');
}
});
});
</script>
---------------------
HTML code
--------------------
<table class="git-role-table">
<tr><td>
<select id="surveyoptions" >
<option value="opt1">Option 1</option>
<option value="opt2">Option 2</option>
<option value="opt3">Option 3</option>
<option value="opt4">Option 4</option>
</select>
</td>
<td><input type="checkbox" id="Launcher" value="Launcher"> Lau ncher  ; &nb sp;
<input type="checkbox" id="Preparer" value="Preparer"> Pre parer  ; &nb sp;
<input type="checkbox" id="Reviewer" value="Reviewer"> Rev iewer  ; &nb sp;
</td>
<td>
<input type="button" id="btn_addrole" value="Add Role" />
</td>
</tr>
</table>
<div id="role_container">
<table id="rolestable">
<tr><td> </td></tr>
</table>
</div>
If one checkbox is checked, I am able to add a table row. I need help in adding multiple rows at one time if multiple checkboxes are checked.
Please help.
Thank you
sshivanna
So far this is my code...
SCRIPT
<script type="text/javascript">
$(function() {
$("#btn_addrole").click(fu
if ($("#Launcher").is(":check
$("#rolestable > tbody:last").append('<tr><
}
});
});
</script>
---------------------
HTML code
--------------------
<table class="git-role-table">
<tr><td>
<select id="surveyoptions" >
<option value="opt1">Option 1</option>
<option value="opt2">Option 2</option>
<option value="opt3">Option 3</option>
<option value="opt4">Option 4</option>
</select>
</td>
<td><input type="checkbox" id="Launcher" value="Launcher"> Lau
<input type="checkbox" id="Preparer" value="Preparer"> Pre
<input type="checkbox" id="Reviewer" value="Reviewer"> Rev
</td>
<td>
<input type="button" id="btn_addrole" value="Add Role" />
</td>
</tr>
</table>
<div id="role_container">
<table id="rolestable">
<tr><td> </td></tr>
</table>
</div>
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Is it what you need?
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script language=javascript>
$(function() {
$(".btn_addrole").click(function(){
if ($(this).is(":checked") == true)
{
var table=$(".git-role-table").get(0);
var row=document.createElement("tr");
cell=row.insertCell(row.length);
cell.innerHTML=$("#surveyoptions option:selected").val();
cell=row.insertCell(row.length);
cell.innerHTML=$(this).val();
table.appendChild(row);
}
});
});
</script>
</head>
<body>
<table class="git-role-table">
<tr><td>
<select id="surveyoptions" >
<option value="opt1">Option 1</option>
<option value="opt2">Option 2</option>
<option value="opt3">Option 3</option>
<option value="opt4">Option 4</option>
</select>
</td>
<td><input type="checkbox" id="Launcher" value="Launcher" class="btn_addrole"> Launcher
<input type="checkbox" id="Preparer" value="Preparer" class="btn_addrole"> Preparer
<input type="checkbox" id="Reviewer" value="Reviewer" class="btn_addrole"> Reviewer
</td>
<td>
<input type="button" id="btn_addrole" value="Add Role" />
</td>
</tr>
</table>
<div id="role_container">
<table id="rolestable">
<tr><td> </td></tr>
</table>
</div>
</body>
</html>
ASKER
Thank you for your responses. I am sorry I didn't explain my challenge correctly.
Here is what I want.
A user can select one of the options from the select list and can check one or more of the checkboxes before he clicks the 'Add Role' button. For each checkbox checked, I need to insert a row into the table.
When the user checks only one of the checkboxes, that is not a problem. I am able to add a row.
What I really need is to check which of the checkboxes are checked and add a row for each checked checkbox with its value.
So if the user checks 3 checkboxes and select an option, I need to insert 3 rows into the table.
Here is what I want.
A user can select one of the options from the select list and can check one or more of the checkboxes before he clicks the 'Add Role' button. For each checkbox checked, I need to insert a row into the table.
When the user checks only one of the checkboxes, that is not a problem. I am able to add a row.
What I really need is to check which of the checkboxes are checked and add a row for each checked checkbox with its value.
So if the user checks 3 checkboxes and select an option, I need to insert 3 rows into the table.
ASKER
Thank you guys for your assistance. The problem is solved. Here is the solution to the problem.
I had to create a function and call the function to add roles.
<script type="text/javascript">
$(function() {
$("#btn_addrole").click(fu nction(){
if ($("#Launcher").is(":check ed")) {
addRole('Launcher');
}
if ($("#Reviewer").is(":check ed")) {
addRole('Reviewer');
}
if ($("#Preparer").is(":check ed")) {
addRole('Preparer');
}
});
function addRole(roleId){
$("#rolestable > tbody:last").append('<tr>< td>'+$("#s urveyoptio ns option:selected").val()+'< /td><td>'+ $("#"+role Id).val()+ '</td></tr >');
}
});
</script>
I had to create a function and call the function to add roles.
<script type="text/javascript">
$(function() {
$("#btn_addrole").click(fu
if ($("#Launcher").is(":check
addRole('Launcher');
}
if ($("#Reviewer").is(":check
addRole('Reviewer');
}
if ($("#Preparer").is(":check
addRole('Preparer');
}
});
function addRole(roleId){
$("#rolestable > tbody:last").append('<tr><
}
});
</script>
TS, did you test my code in the first post?
ASKER
@sam2912, Thank you for pointing it out. I totally missed it. That worked. Thank you.
Open in new window