Home > Software engineering >  Unable to select first element in JQuery
Unable to select first element in JQuery

Time:10-13

I am unable to execute this code, i want to select the first element id named mark, but unable to select. Any suggestion?

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Select Element by ID</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
    // Highlight element with id mark
    $(".mark").css("background", "yellow");
});
</script> 
</head>
<body>
    <p id="mark">This is a paragraph.</p>
    <p id="saly">This is another paragraph.</p>
    <p id="mary">This is one more paragraph.</p>
    <p id="mony"><strong>Note:</strong> The value of the id attribute must be unique in an HTML document.</p>
</body>
</html>

CodePudding user response:

You are using the class selector in jQuery to get a id.

Try with $("#mark" ) instead

CodePudding user response:

Always remember, that use id with # and class with .

Modify your code, and replace

$(".mark").css("background", "yellow");

with

$("#mark").css("background", "yellow");
  • Related