Solved

Removing rows dynamically with jQuery (currently can only add)

Posted on 2014-11-26
7
72 Views
Last Modified: 2014-11-26
Hi - I can currently add a new row using javacript but what i need to be able to do is also remove the row.
below is the current code to add a row.

<script type='text/javascript'>
//<![CDATA[
 $(document).ready(function() {
  var currentItem = 1;
  $('#addnew').click(function(){
   currentItem++;
   $('#items').val(currentItem);
var strToAdd = '<tr><td width="10px"><input id="theDate'+currentItem+'" size="10" value="<? echo date(" d/m/Y "); ?>" name="theDate'+currentItem+'" type="text"></td><td><select id="Sport'+currentItem+'" name="Sport'+currentItem+'"><option>Horse Racing</option><option>Football</option><option>Greyhounds</option><option>NFL</option></select></td><td><input id="Pick'+currentItem+'" name="Pick'+currentItem+'" type="text"></td><td><select id="Bookie'+currentItem+'" name="Bookie'+currentItem+'"><option>Bet365</option><option>Betfred</option><option>BetVictor</option><option>Boylesports</option><option>Bwin</option><option>Centrebet</option><option>Coral</option><option>Ladbrokes</option><option>Paddy Power</option><option>Pinnacle Sports</option><option>SBOBET</option><option>Sky Bet</option><option>Stan James</option><option>unibet</option><option>William Hill</option></select></td><td><input id="Odds'+currentItem+'" name="Odds'+currentItem+'" size="3" type="text"></td><td><select id="BackorLay'+currentItem+'" name="BackorLay'+currentItem+'"><option>Back</option><option>Lay</option></select></td><td><input id="Stake'+currentItem+'" name="Stake'+currentItem+'" size="3" type="text">E/W<input name="EW'+currentItem+'" ID="EW'+currentItem+'" value="EW" type="checkbox" /></td><td><select id="Com'+currentItem+'" name="Com'+currentItem+'"><option>0</option><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option></select></td><td><select id="Outcome'+currentItem+'" name="Outcome'+currentItem+'"><option>Unknown</option><option>Win</option><option>Loss</option><option>P</option><option>E/W Win</option><option>Void</option></select></td><td><select id="PlaceDiv'+currentItem+'" name="PlaceDiv'+currentItem+'"><option>5</option><option>4</option></select></td></tr>';
   $('#data').append(strToAdd);
   
  });
 });
 
//]]>
</script>

Open in new window


how would i remove a row if row is more then 1?
0
Comment
Question by:runnerjp2005
  • 4
  • 3
7 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 40466607
what is the process to remove?
I don't see you delete button in the row
0
 

Author Comment

by:runnerjp2005
ID: 40466619
I have not added it yet - was awaiting guidance in case i was doing the "add" incorrectly
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40466631
So in the row add a remove button :
<input type="button" class="remove-row" value="REMOVE" />

Open in new window


and add this code :
$("#data").on("click", ".remove-row", function(evt) {
     $(this).closest("tr").remove();
});

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:runnerjp2005
ID: 40466647
I added the code and it does not remove
0
 

Author Comment

by:runnerjp2005
ID: 40466648
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40466653
the remove button is to ad on each line your add (line 8 in your code, original post/question)
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40466682
and put the code inside document ready block, you should know why
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now