Does jQuery have a method of determining if a particular table exists in a HTML document and alert();
me whether it does or does not exist?
The particular tables' existence I'm looking for in the document is:
<table id="main_table"
Does jQuery have a method of determining if a particular table exists in a HTML document and alert();
me whether it does or does not exist?
The particular tables' existence I'm looking for in the document is:
<table id="main_table"
Share
edited Nov 4, 2017 at 23:06
Brian Tompsett - 汤莱恩
5,89372 gold badges61 silver badges133 bronze badges
asked May 22, 2012 at 11:04
derek8derek8
2691 gold badge5 silver badges11 bronze badges
3 Answers
Reset to default 8$(function() {
// after dom ready
if($('table#main_table').length){
alert('exists');
}
})
at domready
event just check
$(function() {
if ($('#main_table').length) { ... /* element exists */ }
/**
* or - without passing again through jQuery function -
* if (document.getElementById('main_table')) { ... }
*/
});
$(document).ready(function() {
$("#check").click(function() {
if($('table#main_table').length){
alert('Table Exists');
}
});
});
Example: http://jsfiddle/ipsjolly/3nVrZ/2/