Assign a class to the list item that has a designated ID

I am attempting to assign a class to a specific li element based on its id. Below is the code I have tried:

function updateIndex(indexValue){
    $('li[id=' + indexValue + ']').addClass("selectedQuestion")
}

However, I seem to be encountering an issue where the indexValue variable is not accessible within the li selector.

Answer №1

To utilize the value of the indexValue parameter, you must perform a string concatenation:

$('li[id=' + indexValue + ']').addClass("selectedQuestion")

Alternatively, if you are targeting an id attribute:

$('li#' + indexValue).addClass("selectedQuestion");

If the id is unique, then you can omit the 'li' part (unless you specifically want to target an li element with that id):

$('#' + indexValue).addClass("selectedQuestion");

Answer №2

function highlightSelectedQuestion(index){
   $('li[id=' + index +']').addClass("selectedQuestion")

}

Answer №3

For optimal performance, replace your existing code with this snippet. Ensure that the variable indexValue is properly concatenated.

function updateIndex(indexValue){
  $('li[id=' + indexValue + ']').addClass("selectedQuestion")
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a new div element with a specific Id and appending it to an already

I'm having trouble adding a new div element within an existing div using JavaScript. Here's the code I'm currently using: HTML: <div id="hashtag-content-footer"> JavaScript: <script type="text/javascript> $("#hashtag-content- ...

stream a song with a font awesome symbol

Can an audio track be played using a font awesome icon, such as displaying the song name (mp3) with a play icon right next to it? When users click on the play icon, can the track start playing and be paused or stopped at will? The list will consist of app ...

Are the topmost features of Winforms controls also available in Asp.net webform controls?

Is it possible to create a persistent control group that remains on the screen even when a different webpage is open, while my webpage is active in another tab using ASP.NET? Similar to Winform's topmost feature. ...

Leveraging summernote in meteor with Discover Meteor tutorial

Recently, I've been delving into the world of Discover Meteor and encountering some challenges along the way. My current hurdle involves integrating content entered in summernote into mongo, but it seems to be more complicated than expected. The fil ...

Steps to access the most recent eventName (determined by date)

I am working with two arrays var events=["DELIVERED", "OUT TO DELEVERY", "REACHED WAREHOUSE", "DEPARTED"]; var eventDetails= [{ "source" : "application" "DateTime": "2016-05-12 11:20:00", "eventName" : "DELIVERED" }, { "source" : "application" "DateTime" ...

Having trouble incorporating a Vue.js element onto a Blade template

I am currently experiencing an issue with loading a Vue.js component on my Blade engine. After adding the Vue.js component, it only loads either the Vue.js 'bootstrap' or the public/js/app.js file depending on what is commented out in the app.bla ...

Is there a way to load the right amount of images in advance, depending on the size of the

I'm currently trying to optimize the loading of Largest Contentful Paint (LCP) candidate images based on device size, but I'm encountering difficulties. The issue at hand: On smaller screens, only one image is displayed in a carousel On larger ...

Interacting with jQuery through live events and handling callbacks when using WCF services

Currently, I am developing a web application using asp.net, c#, and jquery. The majority of my work involves generating dynamic HTML content for the browser and utilizing various web services to fetch the necessary data. One of my service calls looks like ...

Using jQuery to update a label within a checkbox list

I'm facing a challenge in jQuery where I am attempting to dynamically replace labels with hyperlinks. Unfortunately, my current code doesn't seem to be working as expected. My goal is to assign a specific hyperlink to each list item based on its ...

Focusing on the content within an anchor tag while excluding the image

How can I prevent the picture from being underlined while ensuring that the hyperlink text remains underlined? The website is built on Wordpress theme, so I am restricted to using CSS only and cannot modify the HTML code. .post-desc a{ border-bottom ...

Turn off escape option when PointerLockControls are in use

Is there a way to prevent the ESCAPE option from being activated (when using PointerLockControls and ThreeJS) by pressing the escape key on the keyboard? I have a different function in mind for this key in my project! Appreciate any assistance in advance ...

Adjust the size of the container, not the image output - Croppa Vue component

Looking for a solution to crop an image for a carousel within a form? The croppa container is set at 300*167 px to fit the form. However, resizing the output image using the "quality" property leads to loss of image quality due to stretching. Is there a wa ...

increased use of ajax on a single page for the dynamic display of additional information

On my webpage, I have a section that showcases various posts. Whenever I click on the "other article" button, I use JavaScript to count the number of articles within the main div and then pass this value to a PHP script using an AJAX call. //JS (ajax) $(d ...

Utilize the power of Nuxt and Vue 3 to create sortable columns in a table, with the ability to only change the icons on the sorted column

I am facing a challenge with my sortable table icons. Whenever I click on an icon to sort a column, all the other icons also change direction. I understand that this is happening because I am toggling the visibility of icons based on the currentSortDir sta ...

In Jasmine, anticipate that an array of floating point values will be similar to another array

Testing a JavaScript function that returns an array of numbers involves checking if the returned array contains the same elements as the expected output: expect(myArray).toEqual(expectedArray); The comparison works well with arrays containing only intege ...

Integrating Endless Scrolling feature into current website

We are currently working on incorporating infinite scroll functionality into our existing website. Currently, we have an image that says 'click for more' which triggers an ajax call to the method GetArticlesFromNextSection(true). This method retu ...

Error Message When Attempting to Load JQuery Library

Currently experimenting with PHP, JavaScript, and jQuery tutorials, I encountered the following error. The tutorials can be accessed here Below is the error message from the Chrome browser console: Failed to load resource: net::ERR_FILE_NOT_FOUND auto- ...

Verify the presence of blank space with javaScript

I currently have a text box on my website. <input type="text" name="FirstName" value="Mickey"> My goal is to prevent the user from entering empty spaces, tabs, or new lines in the text box. However, I want to allow spaces between characters. Does a ...

The server is converting numeric JSON data types to strings

Currently, I am loading a CSV file, parsing it into a JSON object, and then converting the strings into numbers. Although these are displayed as numbers in the browser console, when I send the data to the server using AJAX, everything is interpreted as str ...

An error stating 'This is not defined' occurs when using componentDidMount()

I am encountering a TypeError: 'this' is undefined when attempting to use it in my componentDidMount method. I have confirmed that I properly binded the method. Could there be an issue with how I bound the method, or is there another underlying p ...