Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8120971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:19:55+00:00 2026-06-06T05:19:55+00:00

i am trying to add class on on-click jquery event for that i am

  • 0

i am trying to add class on on-click jquery event for that i am doing

my html file

<head>
        <style type="text/css">
            .red{ color:red; }
        </style>
         <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript" src="js.js"></script>
    </head>
<body>
    <ul id="list">
        <li><a href="">1</a></li>
        <li><a href="">2</a></li>
    </ul>
</body>

javascript file js.js is

$(document).ready(function(){
     $("li").onclick(function(){ // if i comment this and only add the line billow the all ul>li will red
        $("#list").addClass("red");
    });
});

when i click on the li the color of li will become the red the thing makes me confuse that on-click i am adding the class .red to the ul so the both link should be red but on click only the link i clicked become red

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T05:19:56+00:00Added an answer on June 6, 2026 at 5:19 am

    You’re confusing jQuery’s .click() method with the vanilla JavaScript element.onclick = functionRef;

    In your code sample, you want:

    $("li").click(function(){
        $("#list").addClass("red");
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to append a large block of text using jquery's append(). $('#add_contact_btn').click(function(event) {
I'm trying to add a class to some input elements depending on the type,
I'm trying to add a class (.active) to a text field once the user
I'm trying to add click event handlers to a bunch of dynamically generated divs
I'm trying to add an function to an a click event to my code.
I have this HTML file with some CSS styles, now I am trying to
Trying to add a class object into a List using reflection, but when invoking
I am trying to add a class of 'nocheck' to a specific image with
Trying to add a dynamic class value to one of my columns in CGridView:
I'm trying to add an extra class tag if an element / value is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.