I just started trying to learn JQuery. I wanted to make a
change color on anchor click, but this code isn’t working and I can’t figure out why. I know this is very basic but it is the first step to me learning JQuery.
main.js :
jQuery(function ($) { $('#test').click(function () { $("p").css("color", "yellow"); }); });
index.html:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title></title> <link rel="stylesheet" href=""> </head> <body> <a id="test" href="#"> <span>test</span> </a> <p> Hello </p> <script src="main.js"></script> </body> </html>
Answer
You should import jQuery library.
<head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head>
Ref: https://www.w3schools.com/jquery/jquery_get_started.asp