If there is css present, then it is a conditional statement

Is there a way to determine if a div has specific CSS properties?

Below is an attempt that did not yield the desired outcome:

$("#red").click(function() {

if( $("#red").css("top") == "10") {
$("#red").css("top", "100");
} else {
$("#red").css("top", "10");
}

/*
if( $("#red").css("background-color") == "red") {
$("#red").css("background-color", "grey");
} else {
$("#red").css("background-color", "red");
}
*/

    });
#red{
position: absolute;
top:10px; left:10px;
width:100px; height:100px;
background-color:red;
cursor:pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="red"></div>

Answer №1

Did you remember to check for pixels?
For instance:

css("top") == "10px"

Instead of

css("top") == "10"

Regarding the background-color, when using .css it returns as rgb.

$("#red").click(function() {
  var redColor = "rgb(255, 0, 0)";
  var greyColor = "rgb(128, 128, 128)";

  if ($(this).css("top") == "10px") {
    $(this).css("top", "100px");
  } else {
    $(this).css("top", "10px");
  }

  if ($(this).css("background-color") == redColor) {
    $(this).css("background-color", greyColor);
  } else {
    $(this).css("background-color", redColor);
  }
});
#red {
  position: absolute;
  top: 10px;
  left: 10px;
  width: 100px;
  height: 100px;
  background-color: red;
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js></script>

<div id="red"></div>

Update
I'd suggest a different approach by using css classes instead of inline styling.

Here's a simple example for this scenario:

$("#box").click(function() {
  if ($(this).hasClass('up')) {
    $(this).removeClass('up');
    $(this).addClass('down');
  } else {
    $(this).removeClass('down');
    $(this).addClass('up');
  }
});
#box {
  position: absolute;
  width: 100px;
  height: 100px;
  cursor: pointer;
}

.up {
  top: 10px;
  left: 10px;
  background-color: red;
}

.down {
  top: 100px;
  background-color: grey;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="box" class="up"></div>

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

What is the best way to access the validation group in a javascript event?

Recently, I've been grappling with a challenge involving a button that has a validation group. The quirk is that the button click event triggers a jQuery function without using onclientclick. As I navigate through this dilemma, one burning question re ...

How can I position the arrows inside the Slick Slider instead of outside?

After discovering how user-friendly slick slider is, I decided to incorporate it into a website I have been developing: You can find the slider positioned at the bottom of the page. However, I am facing an issue where I would like the navigation arrows to ...

Positioning the label for a Material-UI text field with an icon adornment when the shrink property is set

https://i.stack.imgur.com/E85yj.png Utilizing Material-UI's TextField, I have an Icon embedded within. The issue arises when the label "Your good name here" overlaps the icon instead of being placed beside it. This problem emerged after I included ...

"TipTip functionality seems to be activated only upon the second hover following an ajaxpost

Issue: Despite tooltips functioning properly on the main page, they fail to work within a fancybox or after an ajax post. Attempts to reinitialize TipTip using fancybox callbacks proved ineffective. UPDATE Title modifications After some experimentation, ...

Render real-time webpage elements depending on the URL parameter (Using Parse Cloud, Express, ejs)

As a newcomer to programming, I have embarked on a project to create a web app using Parse.com Cloud Code and Express. The structure of my project is as follows: app.js (Containing Express related codes) views/hello.ejs (Template file) main.js (Clo ...

Is there a way to deactivate the multi selection feature in jsTree?

I encountered an issue while using the jsTree plugin. I created a common piece of code that utilizes the class selector for this plugin. However, when I applied the plugin to another page, I noticed that the single selection feature was not working as inte ...

Troubleshooting SCSS Compilation Problems in Vue.JS

I am currently working on a vue.js web application and I want to incorporate SCSS. To do this, I have installed npm i node-sass sass-loader. Additionally, I have created a vue.config.js file at the root level of my project: module.exports = { css: { ...

Numerous categories housed within various div containers

I am working with an HTML code that contains 6 different div elements: <div class="hisclass1 hisclass2 myclass hisclass3"> <div class="herclass1 herclass2"> <!-- 2nd div --> </div> </di ...

Is there a way to align the image to the left within the div contained in my header section?

I am facing an issue with positioning the logo in the header section of my website. It appears correctly on the left side for tablet and mobile versions, but on desktop, it does not align to the corner as desired. I tried adding a right margin, but I belie ...

Is it possible to use JavaScript to clear a field that was generated by Yii CRUD?

Issue with Yii's GRUD field generated and Firefox HTML: <?= $form->field($model, 'productId')->textInput(['maxlength' => true]) ?> Comparison of PHP generated code to Firefox HTML: <input id="taolistforcreate-p ...

Querying with jQuery to Retrieve HTML from Elements with the Same Class

Greetings Everyone! Please take a look at my code... <font class="detDesc" > Uploaded 07-11&nbsp;2012, Size 105.79&nbsp;MiB, ULed by <a class="detDesc" href="/somelink" title="somelink title">somelink</a> </font> ...

What could be the reason my view is not updating with the use of a js.erb file in Rails 4?

I found a helpful guide on this blog to implement a toggle feature for an attribute in my database. While the toggling functionality is working fine, I'm struggling to update my view to reflect the changes. This is my first time using js.erb files so ...

Having trouble with jquery's empty() function not functioning as expected

I'm brand new to using jquery so I apologize in advance for any beginner mistakes. Essentially, my issue is straightforward: Here is the HTML code I have: <div class="v" align="center" id="div4"> <div id="div5" class="h blurb"> <sp ...

JavaScript function that generates a unique object by extracting values from SerializeArray

I have extracted an array using the serializeArray() function and now I need to pass these key/value pairs to a specific JavaScript object. The result from $("selector :pseudoSelector").serializeArray() is as follows: This array contains: [ { "nam ...

Eliminate viewed alerts by implementing a scrolling feature with the help of Jquery, Javascript, and PHP

My goal is to clear or remove notifications based on user scrolling. The idea is to clear the notification once the user has seen it, specifically in a pop-up window for notifications. I am relatively new to Javascript/jQuery and feeling a bit confused abo ...

What is the best way to retrieve each input value in JavaScript and then send it to the AJAX data?

This Form Belongs to Me <form method="post" id="BargainForm"> <input type="hidden" name="pro_id" class="pro_id" id="pro_id" value="<?php echo $pro_id; ?>"> <input type="hidden" name="customer_id" class="customer_id" id="custom ...

Display or conceal the nearest element that was clicked

http://jsfiddle.net/KevinOrin/xycCx/1/ jQuery('.happening-main-text .readmore').click(function() { jQuery('.divmore').toggle('slow'); return false; }); ...

A simple guide on how to surround every incorrect input index in mapped inputs with red borders

I am incorporating a modal that corresponds each element of the object newCompanies to a specific row: {newCompanies.map((company, index) => { return ( <div> <div className="side- ...

Loading New Page When Iframe is Loaded

I am currently working with an iframe that reflects the appscript I have created. Is there a way to automatically redirect the user to a different page when they change a link or submit a form within the iframe? <div class="embed-responsive em ...

Combining numerous done() callbacks to a single deferred promise

Basically, I am looking to implement a single general callback that triggers every time an ajax call is successful, with additional specific callbacks based on where the method is called. It appears to be functioning properly. My query pertains to whether ...