How can I perform a "click" action on all the rows that start with "label style-scope ytcp-button" in the following html:
<div class="label style-scope ytcp-button">Edit Draft</div>
Here what i tried
$("div[class^=label style-scope ytcp-button]").click();
it seem not work.
CodePudding user response:
You can use the simple JS method querySelectorAll()
.
const els = document.querySelectorAll('.label.style-scope.ytcp-button');
els.forEach((el) => {
el.click();
console.log('clicked');
});
CodePudding user response:
You have missed the Apostrophe. You should write like this:-
$("div[class^='label style-scope ytcp-button']").click();
Run the code snippet below, it will not run alert method on clicking on the last div as the "ytcp-button" class is missing in the last div.
$(document).ready(function(){
$("div[class^='label style-scope ytcp-button']").click(function(){
alert("You have Click a Specific Class");
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="label style-scope ytcp-button">It will Show Alert</div>
<div class="label style-scope ytcp-button">It will Show Alert</div>
<div class="label style-scope ytcp-button">It will Show Alert</div>
<div class="label style-scope">It will not Show Alert</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj 3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script src="script.js"></script>
</body>
</html>