Solved

Removing rows dynamically with jQuery (currently can only add)

Posted on 2014-11-26
7
73 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!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

830 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