Solved

Removing rows dynamically with jQuery (currently can only add)

Posted on 2014-11-26
7
75 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

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

Report: Liquid Web beats Amazon, Rackspace & More

A study by performance analyst firm Cloud Spectator finds that Liquid Web beats rivals Amazon, Rackspace and DigitalOcean when it comes to website and cloud application performance.

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

732 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