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 7791951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:03:22+00:00 2026-06-01T22:03:22+00:00

I wonder how I can add a CSS class to a link based on

  • 0

I wonder how I can add a CSS class to a link based on the ID in the link, with jQuery. I wan’t to add the CSS class “active” to the link if the if-statement is true. Not that I dont want to remove “pFavorite” from class in link if the statement is true, I just want to add active to, like this class=”pFavorite active” Im not that in to jQuery yet. I hope my code still explains what I want to achieve.

<?php
foreach($statement as $p)
{
    if(array_search($p['id'], explode(",", $_COOKIE['cookie'])))
    {
    ?>
    <script type="text/javascript">
        $("#<?php echo $p['id']; ?>", ".pFavorite").addClass("active");
    </script>
    <a href="#" class="pFavorite" id="<?php echo $p['id']; ?>">IMG HERE</a>
<?php 
    } 
}
?>
  • 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-01T22:03:23+00:00Added an answer on June 1, 2026 at 10:03 pm

    First of all your jQuery selector is wrong, you probably meant something similar to

    $("#<?php echo $p['id']; ?>.pFavorite").addClass("active");
    

    The above will match the element with the specific id and the class pFavorite, while your original selector would match all elements with the class pFavorite and then look for the element with the specified id inside any of those, not finding anything (because the target element is one of those having the class, not a descendant).

    Second, you don’t need a class selector since you are already using an id selector and ids are meant to be unique. So that would be further simplified to

    $("#<?php echo $p['id']; ?>").addClass("active");
    

    Finally: why do you want to set the class after the page loads with jQuery, when you have all the information you need on the PHP side? You can simply do

    if(array_search($p['id'], explode(",", $_COOKIE['cookie']))) {
        // Use htmlspecialchars for all HTML output; you may need to specify
        // additional parameters (see the function documentation)
        printf('<a href="#" class="active pFavorite" id="%s">IMG HERE</a>',
               htmlspecialchars($p['id']));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wonder if someone can help me. I'm trying to add a reference to
I wonder if I can keep ido from not remembering my history and only
I wonder how I can add mysqldump to my MAMP environment. I googled a
I want to create a DBRef manually so that I can add an additional
i wonder if below, generating content the jQuery 1.4 way $(<div />, { class:
I wonder if there is a service where I can clean-up html-code so that
I always wonder why compilers can't figure out simple things that are obvious to
I wonder how you can add buttons (left and right) to change page controll
I have seen that I can add errors and render them via <form:errors />
Take a look at this screenshot I wonder how can we add different kind

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.