Using jQuery to select elements on a webpage

I'm experimenting with Royal Slide and trying to dynamically change the ID based on image changes. I've attempted using translateX values, but it's been challenging for me since I have limited experience with jQuery/JavaScript. However, I came across another idea when I saw this:

<div class="rsNav rsBullets">
   <div class="rsNavItem rsBullet rsNavSelected"><span></span></div>
   <div class="rsNavItem rsBullet"><span></span></div>
   <div class="rsNavItem rsBullet"><span></span></div>
</div>

I thought utilizing the :nth-child selector would be the best approach, like so:

function colorfondo(){
if($('.RsNav:nth-child(1)').hasClass("RsNavselected")) {
   console.log('works!');
}

Unfortunately, this method did not work as intended.

EDIT Perhaps my explanation was unclear. My objective is something like this:

if (the first child has the rsNavSelected) { do something.}
elseif the second child has the rsNavSelected) { do something.} elseif the third child has the rsNavSelected { do something.} 

RE-EDIT I managed to make it work with the following function: function colorfondo(){

if($('.rsNav div:first-child').hasClass("rsNavSelected")) {
console.log('rojo!');
} 
else if($('.rsNav div:nth-child(2)').hasClass("rsNavSelected")) {
console.log('azul!');
} 
else if($('.rsNav div:nth-child(3)').hasClass("rsNavSelected")) {
console.log('gris!');
} 
}

Now I am looking for suggestions on how to ensure this function runs continuously without stopping.

Answer №1

RsNavselected doesn't match rsNavselected, and RsNav is different from rsNav.

In HTML, attribute values are sensitive to case, but tag names and attributes are not.

To check:

if($('.rsNav:nth-child(1)').hasClass("rsNavSelected")) {
   console.log('works!');
} 

If you need to target the first-child, use the .children() and .first() methods:

if($('.rsNav').children().first().hasClass("rsNavSelected")) {
   console.log('works!');
} 

Answer №2

Another option is to implement it this way:

if($('.rsBullets').find(".rsNavselected").length > 0) {
    console.log('successful!');
} 

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

centered text positioned perfectly on a dynamically adjusting background

Seeking help to center text both vertically and horizontally within a circle background link that needs to be responsive. Setting the line-height equal to the height of the link is not an option, here's my code: html <a class="Previous" href="#"& ...

How to retrieve a value from a getter in a different file using Node.js

Recently, during the creation of a node project, I defined a model as follows: export default class Tokens { constructor() { this.accessToken = ''; this.refreshToken = ''; } getAccessToken() { return ...

Creating a corner ribbon for a Material UI Card: A step-by-step guide

Can anyone provide pointers on incorporating a corner ribbon to a Material-UI card? Currently, I am utilizing makeStyles for styling from Material UI. ...

Opacity effect on input text when it exceeds the input boundaries

I'm having an issue: I need to create inputs with different states, including when the text is longer than the input itself. In this case, the extra text should fade out with a gradient transparency effect: https://i.sstatic.net/r5qQu.jpg Here is my ...

Chip input component in React

I've recently upgraded to React 17 and encountered an issue with chip input fields like material-ui-chip-input not working properly. I've tried various npm packages, but none of them seem to be compatible with React version 17. Does anyone know ...

Error encountered when attempting to send JSON data with special characters via a POST or PUT request using node.js's http.request()

While attempting to use the node.js http module to PUT a JSON data into CouchDB, I encountered an issue. The JSON included a special character "ä" which caused CouchDB to respond with an "invalid_json" error. However, once the special character was remove ...

The custom checkbox feature is not appearing on Google Chrome browser

I need help creating a custom checkbox in HTML using CSS. It works fine in IE, but in Google Chrome, it's displaying the default checkbox instead of my custom one. What changes do I need to make for it to work in Chrome? HTML <link href="btn.css ...

Setting a setTimeout function for a Vue function

Can clearTimeout be used for a setTimeout function assigned to a Vue Method? If yes, how can it be accomplished? methods: { timeoutController() { setTimeout(function() { this.controllerShown = false; }, 3000); ...

Substituted the BR tag with a comma while including a space after the text

Upon further examination, I have observed that an extra space is being added when replacing the BR tag with a comma in the text: HTML: <div> one first <br class="hey"> two second <br class="hey"> three third </div& ...

Create a rectangular container with text using CSS

Looking to achieve a square box design using CSS with specific dimensions? Want to insert text inside the square and ensure it is horizontally centered within the square? Check out this code snippet: #myid { width: 100px; height: 100px; ...

Irritating Popup - (or perhaps a more elegant alternative)

My current challenge involves tracking the time a user spends with a specific window in focus. I aim to record this duration accurately and pause the timer when the user switches to another window, then resume it upon returning. Ultimately, I want to measu ...

How do you manage dependencies for nested components within Angular2?

Encountering an Issue: browser_adapter.js:76 Error: Cannot resolve all parameters for NestedComponent(undefined). Make sure they all have valid type or annotations. at NoAnnotationError.BaseException [as constructor] Diving Deeper Into the Problem: ...

Switch the color of material icons when hovered over

Currently experimenting with this demo and I'm looking to adjust the color of icons when hovered over .demo-navigation .mdl-navigation__link .material-icons:hover { background-color: #00BCD4; color: #FFF; } Unfortunately, my attempt was unsucces ...

The border class in Bootstrap 4 seems to be malfunctioning when it comes to the top

I'm struggling to add a border to a div using Bootstrap 4. When I try using only the border property, it looks strange, and when I use other properties, no border appears. Here is the code snippet: I even tried with the beta 2 version. If there is s ...

Produce a visually stunning highchart graphic with the power of angular.js

I've been struggling for the past 4 hours trying to get this thing to function properly. I'm pretty new to angular.js, jquery, highcharts, and all that jazz, and I just can't seem to figure out how to resolve this. What I'm attempting ...

Switching on and off a class in Next.js

I'm a beginner with Next.js and React framework. My question is regarding toggling a class. Here's my attempt in plain JS: function toggleNav() { var element = document.getElementById("nav"); element.classList.toggle("hidde ...

Displaying a project in the same location using jQuery

Struggling with jQuery and positioning hidden items that need to be shown? I have two white boxes - the left one is the Client Box (with different names) and the right one is the Project Box (with different projects). My goal is to show the projects of a c ...

Initiate a jQuery function once the document is fully loaded

Is there a way to apply the Javascript code within my .change() function to the same function in the .ready() function without duplicating the code? $('.selabw').change(function(){ let that = $(this); let value = that.parent().next(). ...

What is the best approach for managing routing in express when working with a static website?

Whenever a user navigates to mydomain.com/game, I aim for them to view the content displayed in my public folder. This setup functions perfectly when implementing this code snippet: app.use('/game', express.static('public')) Neverthel ...

Eliminate the golden hue from the input field when autofill is activated

Can anyone help me figure out how to get rid of this unsightly chrome background that appears when autofill is enabled? (See below for reference.) I've attempted the following solutions: *:focus { outline: 0; } input:-webkit-autofill { -webk ...