Emilia Clarke
Emilia Clarke

Reputation: 83

.click not working for <p> elements

I want to know when a paragraph element has been clicked. I get no response when clicking p elements with this code.

HTML

<!DOCTYPE html>
<html>
<title>Practice Extension</title>
<head> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type ="text/javascript" src="popup.js"></script>
</head>
<body>
    <p style= "color: red">red</p>
    <p style= "color: blue">blue</p>
    <p>black</p>
</body>
</html>

JQuery

function main() {
    $("p").click(function () {
        alert("a p has been clicked!");
    })
}
$(document).ready(main());

Upvotes: 0

Views: 163

Answers (1)

afuous
afuous

Reputation: 1498

Instead of

$(document).ready(main());

Use

$(document).ready(main);

main is a function, and $(document).ready expects a function as a parameter. If you pass it main() instead of main, you are passing it the result of calling the function (undefined) instead of the function itself.

Upvotes: 2

Related Questions