.length
PropertyYou can use the jQuery .length
property to determine whether an element exists or not in case if you want to fire some event only if a particular element exists in DOM.
Here's an example that displays an alert on button click if the specified element exists.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Test If an Element Exists</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
if($("#myDiv").length){
alert("The element you're testing is present.");
}
});
});
</script>
</head>
<body>
<div id="myDiv"></div>
<p>The quick brown fox jumps over the lazy dog</p>
<button>Check Element</button>
</body>
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on harsukh21@gmail.com
CRUD Operation using PHP7 | MySql | PDO
This is a step by step PHP 7 & MySQL...How to Convert Comma Separated String into an Array in JavaScript
Use the split() Method You can use th...How to forEach Over an Array in JavaScript
Use the forEach() Method You ca...How to get all the keys of an associative array in PHP
Use the PHP array_keys() funct...How To Create Custom Helper In Laravel 5.5
Today in this article we are share with...