Reputation: 7520
I need a little help here about creating my form.
I am creating a resume form. And I have a part that can add or remove academic background. Here's the scenario.
That's the scenario. Here's my sample code.
<tr style="background-color: orange">
<td colspan="2">
<label style="font-size: 14px">Academic Background</label>
</td>
</tr>
<div id="academic">
<tr>
<td><label style="font-weight: normal"><label style="color: red">*</label> Highest Qualification: </label><br /><br /</td>
<td>
<select class="form-control" name="highest_qualification[]">
<?php
for($y = 0; $y < sizeof($qualification); $y++){
echo "<option value='".$qualification[$y]."'>".$qualification[$y]."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td><label style="font-weight: normal"><label style="color: red">*</label> Field of study: </label><br /><br /></td>
<td>
<select class="form-control" name="field_study[]">
<?php
foreach($field_study as $key=> $val){
echo "<optgroup label='".$key."'>";
foreach ($val as $option) {
echo "<option value='".$option."'>".$option."</option>";
}
echo "</optgroup>";
}
?>
</select>
</td>
</tr>
<tr>
<td><label style="font-weight: normal"><label style="color: red">*</label> Grade: </label><br /><br /></td>
<td>
<select class="form-control" name="grading[]">
<?php
for($g= 0; $g < sizeof($grading); $g++){
echo "<option value='".$grading[$g]."'>".$grading[$g]."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td><label style="font-weight: normal"><label style="color: red">*</label> Institute / University: </label></td>
<td><input type="text" class="form-control" name="university[]" /></td>
</tr>
<tr>
<td><label style="font-weight: normal"><label style="color: red">*</label> Located In: </label></td>
<td><input type="text" class="form-control" name="location[]" /></td>
</tr>
<tr>
<td><label style="font-weight: normal"><label style="color: red">*</label> Graduate Date: </label></td>
<td>
<label style="font-weight: normal">Month</label>
<?php
$month = array('January','February','March','April','May','June','July','August','September','October','November','December');
echo "<select name='graduate_month[]'>";
for($p = 0; $p < sizeof($month); $p++) {
echo "<option value='".$p."'>".$month[$p]."</option>";
}
echo "</select>";
?>
<label style="font-weight: normal">Day</label>
<?php
echo "<select name='graduate_day[]'>";
for($l = 1; $l <=31 ; $l++) {
echo "<option value='".$l."'>".$l."</option>";
}
echo "</select>";
?>
<label style="font-weight: normal">Year</label>
<?php
echo "<select name='graduate_year[]'>";
for($u = 2014; $u >= 1978 ; $u--) {
echo "<option value='".$u."'>".$u."</option>";
}
echo "</select>";
?>
</td>
</tr>
</div>
<tr>
<td></td>
<td>
<input type="button" value="+" class="btn" name="new_academic"/>
<input type="button" value="-" class="btn" name="del_academic" />
</td>
</tr>
I looked for some examples in the Google and what I found is something like this.
<div class="demo-container">
<div class="demo-box">Demonstration Box</div>
</div>
$( "div.demo-container" ).html();
This is the example of jquery. The output will be:
<div class="demo-box">Demonstration Box</div>
How can I apply this code? Can you provide me an example how to do it? Thats all thanks. By the way I am a beginner in jquery that's why I have a difficulty in doing it.
Upvotes: 0
Views: 202
Reputation: 713
Directly underneath the div with id academic
, create a new div with a class that isn't used elsewhere in the application.
When the +
button is clicked, clone this newly created div, putting it inside the academic
div.
Here is an example http://jsfiddle.net/bbankes/MvgMg/
<div id="academic">
<div class="uniqueWrapper">
<div>
<input name="myInput[]" />
</div>
<div>
<input name="myOtherInput[]" />
</div>
<button class="add">+</button>
<button class="remove">-</button>
</div>
</div>
$(document).on('click', '.add', function () {
var form = $(this).parents('.uniqueWrapper').eq(0);
var formClone = form.clone();
form.after(formClone);
});
$(document).on('click', '.remove', function () {
$(this).parents('.uniqueWrapper').eq(0).remove();
});
Upvotes: 1