Learn the art of separating a CSS attribute and its value with Jquery

I'm currently facing a challenge with splitting a CSS value in jQuery.

I have a styled div like this:

.loadingTank {
  display: inline-block;
  border: 2px solid black;
  margin: 15px;
  max-width: 350px;
  height: 500px;
  width: 30%;
  background-color: #808080;
  box-shadow: inset 0px -200px 0px -2px #383838;
  /*Change this value for leveling*/
  float: left;
  position: relative;
}
.loadingTank:before {
  content: "";
  width: 70%;
  height: 100%;
  float: left;
  background: -moz-linear-gradient(left, rgba(0, 0, 0, 0.4) 0%, rgba(208, 208, 208, 0) 100%);
  /* more CSS properties */
}
<div class="loadingTank"></div>

I want to dynamically move the 'level' of the tank using jQuery.

I am attempting to extract the "-200px" from the following CSS property:

box-shadow: inset 0px -200px 0px -2px #383838;

This jQuery function retrieves the CSS related to this property:

$('#SetValue').click(function () {
            var myVal = $('.loadingTank').css("box-shadow");
            alert(myVal + " is currently the box shadow");
        });

The alert shows:

rgb(56, 56, 56) 0px -200px 0px -2px inset

I only want to extract the "-200" part. How can I do this?


Note: The rest of the CSS line will remain unchanged, but only the "-200px" part will be modified.


Update


I have updated the 'Box shadow' to the correct format:

box-shadow: rgb(56, 56, 56) 0px -200px 0px -2px inset;

However, when trying to retrieve the CSS, the result differs:

In Chrome:

rgb(56, 56, 56) 0px -200px 0px -2px inset

In IE 11:

inset 0px -200px 0px -2px rgb(56, 56, 56)

In Firefox:

rgb(56, 56, 56) 0px -200px 0px -2px inset

The position of "-200px" varies across these browsers. Find a live demo of the problem - test it on IE and Chrome.

Answer №1

Consider utilizing the .split() method like this :-

myVal = myVal.split(" ")[4]

The code above will result in -200px, but if you only want -200, then you can use parseInt()

myVal = parseInt(myVal,10);

Check it out on Fiddle.

UPDATE :-

If Internet Explorer provides a different css property string, you can detect the browser and if it's IE, then use index [2] else use index [4].

Alternatively, you can experiment with something like this :-

if((myVal.split(" ")[2]).indexOf(")") >= 0){
   myVal = myVal.split(" ")[4]    
}
else{
   myVal = myVal.split(" ")[2]
}

Fiddle

Fiddle

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

Connecting a picture to a web address using the <img class> tag

I'm facing a major issue with the "fade script" I added to my portfolio. It's designed to fade between thumbnails using jQuery and CSS, but it relies on 2 img classes that I can't seem to link properly. Basically, I want to link the image g ...

Ways to display or conceal various divs by employing an if/else statement?

I am aiming to conditionally display various divs based on certain criteria: Show the "x" div if it's not already visible Display the "y" div only if the "x" div is already being displayed Show the "z" div only if both the "x" and "y" divs are alrea ...

Automatically navigate to the bottom of the page by applying the overflow auto feature

I've been working on a chat application using Vue.js. I have set the overflow property to auto for my div element. My goal is to automatically scroll to the bottom of the div so that users won't have to manually click the scrollbar to view the la ...

selector that applies to every single element

I am currently working on a custom listbox feature that should set the selected value in the active textbox. However, I am encountering an issue where it is affecting all the textboxes on the page (two in my example). Here is my JQuery code: $(".txtList") ...

A guide on restricting overflow in React Spring Parallax 'pages'

Currently, I have integrated React Spring Parallax () into my project found at this link: https://codesandbox.io/s/parallax-sticky-scroll-2zd58?file=/src/App.js Upon clicking the button to navigate to the next section, it is noticeable that the image over ...

How can you reference the immediate selector object using jQuery?

I'm currently delving into the realm of jQuery and decided to create a test page equipped with the following code: <a id='encode' href='javascript: void(0)'>encode</a> | <a id='decode' href='javascr ...

Using jQuery AJAX to send a URL as a string parameter

Currently, I have an ajax function that sends a string of variables to my script. However, there is one variable that needs to hold a complete URL along with parameters. In the current setup, var1 and var2 are received as $_POST variables. But I specifica ...

detecting key presses on documents using javascript

I'm having trouble capturing document-level key press events on a webpage. I've tried using the following code: $(document).bind('keydown', 'a', keyevent_cb); It works fine in IE, but it's not consistent in Firefox. I&a ...

traverse a JSON object with JavaScript for a loop

After fetching and parsing data from a database using JavaScript, I have the following code snippet: var categories = <?php echo json_encode($categories); ?>; The 'categories' variable in the source code contains the values: var categori ...

Having trouble dynamically adding HTML elements in JQuery

I am dynamically adding HTML elements during runtime using an AJAX call from a JavaScript file. Currently, I am attempting to use a combo box drop-down element to display a list of data. Here is how I am trying to achieve this in my script: $("#myList") ...

Manipulating Object origin data

I'm unsure if this can be achieved. I am aiming to create a header and footer that remain the same while only switching the content pages. The resources I have at my disposal cover HTML, JS, CSS, and JQuery. Below is a simplified version of my curre ...

The background image is not displaying correctly

I've been struggling to add a background to the menu section at the top of my webpage. I uploaded the image to www.ra3manifesto.co.nf and want the logo, h1, and menu bar to be placed on top of it. I attempted to set this background using the following ...

Importing a .js file into HTML with AJAX

The following JavaScript code is located within index.html between "<script type="text/javascript">" function callanular() { peticion_http = new XMLHttpRequest(); peticion_http.onreadystatechange = showContent; peticion_ht ...

The Hamburger Menu in the Bootstrap Navbar is failing to collapse

My website uses Bootstrap v5.3 and I've added a navbar, but the hamburger icon is not collapsing on mobile devices when clicked. Can someone please review my code to see if there are any issues? I can't figure out what's wrong. <!DOCTY ...

The content spills over when applying the Bootstrap Grid system

I am working on creating a display heading that includes images on both the left and right sides of the heading. The layout looks great on larger displays, but as I shrink the display size, the text in the heading overflows the column on the right, causing ...

"Converting jQuery Form into a Wizard feature with the ability to hide specific steps

I'm working on a form where I need to hide a fieldset when a specific event is triggered. Inside the first fieldset, there is a select tag and when a certain option is selected, the second fieldset should be hidden. <form id="form1"> <fi ...

Hiding a div with Javascript when the Excel dialog box is loaded

I have a piece of JavaScript code that is activated when the user clicks on an excel image. $("#excel").on("click", function () { $('#revealSpinningWheel').reveal(); $(window).load(function () { $('#revealSpinningWheel').hide ...

What is the method for asynchronistically loading Material ui fonts?

When using Material-UI, the fonts block the rendering of the first page. If no fonts are loaded, Material-UI defaults to using the Helvetica font until Roboto is downloaded. /signup (localhost) …media/roboto-latin-500.4b218fc7.woff2 (localhost) - 76 ...

It is impossible to replicate the toggle functionality of the navbar-collapse feature from Bootstrap's official website

Currently, I'm delving into Bootstrap by attempting to replicate the 'Company' theme showcased on W3schools. My main struggle lies with getting the navbar-collapse feature to work properly. Despite creating the toggle, clicking on it fails t ...

Implementing CSS keyframes when a specified PHP condition is satisfied

I am looking to implement an opening animation on my website that should only play when a user visits the site for the first time. I want to avoid displaying the animation every time the page is reloaded, so it should only run if a cookie indicating the us ...