I want to have my news
section repeats when i click on add
button using jQuery
and javascript
. it repeats but repeats twice. but i want to repeat it only once, also remove button is not working i mean when i click on remove button there is no action.
need help here is my code,
HTML
<table align="center" cellpadding="0" cellspacing="0" border="0" width="600" class="test" >
<tr>
<td>
<form id="formtwo" name="formone" method="post" enctype="multipart/form-data">
<table width="100%">
<tbody>
<tr>
<td><h3>News</h3></td><td><input type="text" readonly="readonly" value="1" name="newsid" id="newsletter" /></td>
</tr>
<tr>
<td>Title of news:</td>
<td><input type="text" name="newstitle" /><br /><br /></td>
</tr>
<tr>
<td>Description:</td>
<td><textarea rows="5" cols="30" name="description" ></textarea><br /><br /> </td>
</tr>
<tr>
<td>Title of Link:</td>
<td><input type="text" name="titleoflink" /><br /><br /></td>
</tr>
<tr>
<td>URL of News:</td>
<td><a href="#"><input type="text" name="urlofnews" /><br /><br /></a></td>
</tr>
<tr>
<td>News image:</td>
<td><input type="file" name="fileToUpload" id="fileToUpload" ><br /><br /> </td>
</tr>
<tr>
<td></td>
<td align="right"><input class="add" type="button" value="Add" /><input class="remove" type="button" value="remove" /></td>
</tr>
</tbody>
</table>
</form>
</td>
</tr>
</table>
JS
$(document).ready(function(){
$('.add').click(function() {
$(".test").append('<table width="600" align="center"><tbody><tr><td><h3>News</h3></td><td><input type="text" readonly="readonly" value="1" name="newsid" id="newsletter" /></td></tr><tr><td>Title of news:</td><td><input type="text" name="newstitle" /><br /><br /></td></tr><tr><td>Description:</td><td><textarea rows="5" cols="30" name="description" ></textarea><br /><br /></td></tr><tr><td>Title of Link:</td><td><input type="text" name="titleoflink" /><br /><br /></td></tr><tr><td>URL of News:</td><td><a href="#"><input type="text" name="urlofnews" /><br /><br /></a></td></tr><tr><td>News image:</td><td><input type="file" name="fileToUpload" id="fileToUpload" ><br /><br /></td></tr><tr><td></td><td align="right"><input class="add" type="button" value="Add" /><input class="remove" type="button" value="remove" /></td></tr></tbody></table>');
});
});
$('.remove').click(function() {
if($(".test tr").length != 2)
{
$(".test tr:last-child").remove();
}
else
{
alert("You cannot delete first row");
}
});