• DEVHIDE
        • Home (current)
        • About
        • Contact
        • Cookie
        • Home (current)
        • About
        • Contact
        • Cookie
        • Disclaimer
        • Privacy
        • TOS
        Login Or Sign up

        How to add a class to an element that is outside of your onclick element

        354 Views Asked by naroko At 12 June 2015 at 20:05 2025-12-10T00:29:47.369518

        I need some help with some If statement. I have this div

        <div id="features-dots">
        <ul>
            <a href="#"><li class="one"></li></a>
            <a href="#"><li class="two"></li></a>
            <a href="#"><li class="three"></li></a>
        </ul>
        </div>
        

        and

        <div class="feature_thumb">
        <a class="feature_thumb" id="one" onclick="return false;"></a>
        </div>
        <div class="feature_thumb">
        <a class="feature_thumb" id="two" onclick="return false;"></a>
        </div>
        <div class="feature_thumb">
        <a class="feature_thumb" id="three" onclick="return false;"></a>
        </div>
        

        and want to add a class to li.one if #one is clicked, to li.two if #two is clicked and so on...Can someone help me with this?

        javascript jquery if-statement onclick
        Original Q&A
        2

        There are 2 best solutions below

        2
        Jakir Hossain Jakir Hossain On 12 June 2015 at 20:18 BEST ANSWER
        $('a.feature_thumb').click(function(){
            $('.' + this.id).addClass('your-class');
        });
        

        DEMO

        2
        lmgonzalves lmgonzalves On 12 June 2015 at 20:08

        You can use this simple jQuery code:

        $('a.feature_thumb').click(function(){
            $('.' + $(this).attr('id')).addClass('your-class');
        });
        

        DEMO

        Related Questions in JAVASCRIPT

        • Using Puppeteer to scrape a public API only when the data changes
        • inline SVG text (js)
        • An array of images and a for loop display the buttons. How to assign each button to open its own block by name?
        • Storing the preferred font-size in localStorage
        • Simple movie API request not showing up in the console log
        • Authenticate Flask rest API
        • Deploying sveltekit app with gunjs on vercel throws cannot find module './lib/text-encoding'
        • How to request administrator rights?
        • mp4 embedded videos within github pages website not loading
        • Scrimba tutorial was working, suddenly stopped even trying the default
        • In Datatables, start value resets to 0, when column sorting
        • How do I link two models in mongoose?
        • parameter values only being sent to certain columns in google sheet?
        • Run main several times of wasm in browser
        • Variable inside a Variable, not updating

        Related Questions in JQUERY

        • In Datatables, start value resets to 0, when column sorting
        • Bootstrap modal not showing at the desired position on a web page when the screen size is smaller
        • window.location.href redirects but is causing problems on the webpage
        • Using JQuery Date Slider
        • Storing selected language in localStorage
        • How to stop other divs from still showing when i click a different button?
        • Check multiple values with jQuery
        • Bootstrap component does not want to render in Datatables function
        • put white spaces when entering an amount moneytype symfony
        • Trouble accessing custom header in AJAX response using jQuery in Fiware Keyrock
        • I just cant make it work, HTML, JS and Firebase error
        • Didn't declared variable still not getting any error in JavaScript
        • Move element horizontally while scrolling vertically in pure JavaScript
        • allow multi carousel in same page
        • Embedded TikTok posts / thumbnail styling issue

        Related Questions in IF-STATEMENT

        • Basic Python Question: Shortening If Statements
        • Why if condition work but else if condition doesn't work?
        • How to list several items in the dialog box for execution?
        • "if contains" with forbidden special characters
        • bash if equals some file
        • change binary data like "111 into 001" in python by using if else or using regex
        • How much of a bonus do I get if I sell a specific number
        • Conditional Concatenation for Exchange Contact Imports
        • How can i correct a multiple choice question with multiple correct answers?
        • IF AND OR Nested ArrayFormula not working - What am I missing?
        • I can search one sheet, but can't manage to string two searches with the IFS function
        • How to add 1 (or more generally, perform a simple operation) on a column based on a condition
        • Playwright checking the text of a locator and replacing values
        • Read a variable and printing it on a monitor - Arduino
        • Comparison between two strings is not working

        Related Questions in ONCLICK

        • How do i get my close button to work on a popup?
        • astro onclick event not calling the function
        • Not reading the function in a JavaScript Module file, by calling onclick in the html document
        • How do I get the close button to work after popup has appreared?
        • How do I create a popup based on click?
        • I'm did POST method for DELETE with correct parameters and URL in React application but receive 400 Bad request error. Can it be server side issue?
        • In python3-flet script, how to remove a button after click?
        • javascript, why you can't add onClick in HTML string, before you add this HTML string to DOM?
        • Using Javascript to change a div's text depending on what button is pressed
        • Using two buttons with onclick events that continuously change each other?
        • Google Translate not translating without refreshing the page
        • How do I use HTML5 and jQuery to properly point to style sheets using onclick SwapStyleSheet in a folder "css" instead of working all in 1 folder?
        • Why does my onClick event fire every time the React component renders?
        • How to prevent single click when double click on react
        • Why does my Blazor page stop working after succesfully performing an action?

        Trending Questions

        • UIImageView Frame Doesn't Reflect Constraints
        • Is it possible to use adb commands to click on a view by finding its ID?
        • How to create a new web character symbol recognizable by html/javascript?
        • Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
        • Heap Gives Page Fault
        • Connect ffmpeg to Visual Studio 2008
        • Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
        • How to avoid default initialization of objects in std::vector?
        • second argument of the command line arguments in a format other than char** argv or char* argv[]
        • How to improve efficiency of algorithm which generates next lexicographic permutation?
        • Navigating to the another actvity app getting crash in android
        • How to read the particular message format in android and store in sqlite database?
        • Resetting inventory status after order is cancelled
        • Efficiently compute powers of X in SSE/AVX
        • Insert into an external database using ajax and php : POST 500 (Internal Server Error)

        Popular # Hahtags

        javascript python java c# php android html jquery c++ css ios sql mysql r reactjs node.js arrays c asp.net json

        Popular Questions

        • How do I undo the most recent local commits in Git?
        • How can I remove a specific item from an array in JavaScript?
        • How do I delete a Git branch locally and remotely?
        • Find all files containing a specific text (string) on Linux?
        • How do I revert a Git repository to a previous commit?
        • How do I create an HTML button that acts like a link?
        • How do I check out a remote Git branch?
        • How do I force "git pull" to overwrite local files?
        • How do I list all files of a directory?
        • How to check whether a string contains a substring in JavaScript?
        • How do I redirect to another webpage?
        • How can I iterate over rows in a Pandas DataFrame?
        • How do I convert a String to an int in Java?
        • Does Python have a string 'contains' substring method?
        • How do I check if a string contains a specific word?
        .

        Copyright © 2021 Jogjafile Inc.

        • Disclaimer
        • Privacy
        • TOS
        • Homegardensmart
        • Math
        • Aftereffectstemplates