Solved

Removing rows dynamically with jQuery (currently can only add)

Posted on 2014-11-26
7
70 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
Comment Utility
what is the process to remove?
I don't see you delete button in the row
0
 

Author Comment

by:runnerjp2005
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:runnerjp2005
Comment Utility
I added the code and it does not remove
0
 

Author Comment

by:runnerjp2005
Comment Utility
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
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
Comment Utility
and put the code inside document ready block, you should know why
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

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…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

772 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