Remove Second Row From Table - javascript

I would like to remove the second row of my table with jquery.
I have tried:
$("tr:second").remove();
But this unfortunately doesnt work :( Any suggestions?

You need to use
$("tr:eq(1)").remove();
It's zero based, so index 1 is your second row

use nth-child selector
$("table tr:nth-child(2)").remove();

$("tr:eq(1)").remove();
second row has number 1

You can use the ':eq()' selector to achieve this:
// The eq() selector is zero based so the second
// row will be tr:eq(1)
$("tr:eq(1)").remove();
See the documentation at:
http://api.jquery.com/eq-selector/

try $("tr:nth-child(1)").remove()
http://api.jquery.com/nth-child-selector/

Related

Replace the first header text with jquery

I have headers like
<h3 class="left_header">First job partner</h3>
<h3 class="left_header" style="text-align:center;">Don't have an account ?</h3>
Now i want to replace the first header with Job partner. how to do it by jQuery or Java script.
Try to grab the h3 tags with the class .left_header and take the first instance from the element collection using :first selector,
$('h3.left_header:first').text('Job partner')
DEMO
Try this:
$('h3:first-child').html('sublink_active');
Working Demo
Try this
$("h3:first-child").html("Job Partner")
demo
try with this.
$(".left_header:first").html("yourNewTitle");
JQuery :
$('.left_header').first().text("Job Partner");
you can use.first() (http://api.jquery.com/first/)
$( "h3" ).first().html("Job partner");
OR
you can use jQuery( ":first" ) (https://api.jquery.com/first-selector/)
$(".left_header:first").html("Job partner");
fiddle
Your code :
jQuery(".left_header h3").text("Public offers");
your code didn't match with your requirement. Because above code select all h3 elements under the "left_header" class. And In your HTML h3 have an class left_header. So you should use following selector to select elements-
$("h3.left_header");
Now you want to select only first element, so you can use index number like this -
document.getElementsByClassName("left_header")[0].innerText = "Job Partner";
OR
var g = $("h3.left_header");
$(g[0]).text("Job Partner");
Demo1
Demo 2
Note: There are more than one way exist to accomplish this task-
$(".left_header:first").text("Job Partner");
Working demo 1

Remove table rows using jQuery but prevent remove the first row

I am using jQuery to remove table rows - my script works ok but I don't want the button to be able to remove the very first table row. Can anyone suggest how this is done?
$("#remove").click(function(event) {
event.preventDefault();
$("table tr:last").remove();
i++;
});
Try the following:
if ($("table tr").length != 1) {
$("table tr:last").remove();
}
How about
$("tr:last:not(:first)").remove();
You don't need the table selector as all rows are inside tables, but it might be useful to specify the table element from which you want to remove (avoiding side effects if you later would add other tables).
Example: Remove all rows from $table except the first:
$("tr:not(:first)", $table).remove();
You can use gt() and not():
$('table').find('tr:gt(0):last').remove();
This finds all rows with an index greater than 0, gt(0) and selects the last row, :last and then removes that element.
Demo: http://jsfiddle.net/XhtC8/
If you wanted to remove all rows but the first then you can remove :last:
$('table').find('tr:gt(0)').remove();
Try:
$("#remove").click(function(event) {
event.preventDefault();
$("table tr:not(':first')").remove();
i++;
});
Demo here: http://jsfiddle.net/aGukb/

get the first tr that contains tds

In jquery how do I get the first table row that contains <td>s
So far my code looks like this but its not working?
$('#mytable tr').has('td:first').html();
thanks
You could find the first td's parent.
$("#mytable td:first").parent().html();
You could try
$($("#mytable").find('tr')[0]).find('td')
In this case you can iterate through rows and fetch the tds
$($($("#mytable").find('tr')[0]).find('td')[0]).text()
$($($("#mytable").find('tr')[0]).find('td')[1]).text()
Try to use this:
$("#mytable td:first ").parents('tr').html();
Try this, jQuery maintains the element indices while selecting them -
$('#mytable td').eq(0).parent();
So in effect, this will select the first td in your table and return the parent, which should be a tr element, by convention

Hiding specific HTML table cells with JavaScript

I have found other questions through which I learned how to hide the first row and the (first row's) first cell using;
$('table#truetable tr:first').hide();
and
$('table#truetable td:first').hide();
But what if I want to hide for example the first cell of the second row using this approach?
$('#truetable tr:eq(1) td:first').hide();
:eq docs
jQuery(':eq(index)')
index
Zero-based index of the element to match.
$('table#truetable tr:eq(1) td:first-child').hide();
theres a css selector for this:
:nth-child(N)
N is the index of the child +1 in the selection

jQuery nested :eq selector

I'm new to jQuery and I'm trying to select cells in an table. My table have, for example 3 cols with 3 cells each.
So I try to select the first cell of the first row, for example, like this:
$("#table tr:eq(0) td:eq(0)")
But doesn't works! What's the right way to do this ?
Are you sure your table has an ID of table? Or should your selector not have the # in it.
If your table does not have an ID of table and you want to select by tagname, the selector of
$("table tr:eq(0) td:eq(0)");
should work. You can also write it in other ways, such as:
$("table tr:first td:first");
I think the issue might be that you've got an erroneous hash in there though ;)
Try adding this to make sure it's not working (or is)
$("#table tr:eq(0) td:eq(0)").css('background','red').otherStuff();
And does your table have the id table? Try:
$("table tr:eq(0) td:eq(0)").css('background','red').otherStuff();
you could try to use this:
$("#table tr:nth-child(0) td:nth-child(0)")
i didnt test it though.
Edit: nth-child is 1-indexed. so it should be nth-child(1).

Categories