Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to replace #789034 code to another code like #456780 where ever it finds in main.css using jquery

I am having a main.css file something like below :

.common-color
{
  color:#789034;
}

.new-cls
{
  border-color:#789034;
  height:300px;
}

.awesome-one
{
  background-color:#789034;
  height:200px;
  width:300px;
}

I want to replace #789034 code to another code like #456780 where ever it finds in main.css using jquery like :

$(each where code=#789034)
{
  set code: #456780;
}
share|improve this question
    
Maybe you should have a single class having color: #789034; and add this class to all the elements where this color is required. And then it'll be easy to change color using jQuery: $('.myClass').css('color', '#456780'); –  Tushar 1 hour ago
    
its not practically possible, because i am using it on server properties, like border-color:#789034- Think i made a common color class for the code how to pass that class for border color--like 1px solid-- .comon-color. –  techsolver 1 hour ago
    
means ?? actually i clearly said i want to replace a specific color code to other on all places in one go using jquery- Your code selecting attribute, i want only code code to new color code –  techsolver 49 mins ago
    
Really sorry, if i was rude - that was not my intention, really sorry,yes, i it was close and thanks a lot, just it has extra thing i.e attribute, i don't want attribute as parameter. –  techsolver 40 mins ago
    
Possible duplicate of stackoverflow.com/questions/27084411/… (which doesn't have an accepted answer yet either) –  connexo 37 mins ago

3 Answers 3

you might consider changing it with jquery.

$(function() {
  $('.awesome-one').hover( function(){
     $(this).css('background-color', '#789034');
  },
  function(){
     $(this).css('background-color', '#456780');
  });
});
share|improve this answer
    
your code is just for background, i clearly mentioned that it should change at all place where it found #789034 to this #456780- Thanks –  techsolver 1 hour ago

You might want to try the following:

Put your stylesheet inline into the document, wrapped by a style tag:

<style id="myStyles" type="text/css"> /* all your css here */ </style>

Now you can probably get all the contents of this via

var myCss = $("#myStyles").text();

Then go ahead and do the replacement magic:

myCSS = myCSS.replace("#789034", "#456780");

And in a last, hopeful effort, put it back in the DOM:

$("#myStyles").text(myCss);

But even if this should work, it seems to me very probable that you are bringing an XY problem.

share|improve this answer

Why, its elementary, dear Watson. Just grab all possible style-keys, check if they contain the word color, convert them from camel case to hyphenated, then replace all occurrences of the color with the new one using a complex regex system. DUH!

...I have too much free time, sorry:

var keys = Object.keys($('html').get(0).style);
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec('#789034');
var rgb = 'rgb(' + parseInt(result[1], 16) + ', ' + parseInt(result[2], 16) + ', ' + parseInt(result[3], 16) + ')';
for (var k in keys) {
    key = keys[k].replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
    if (key.indexOf('color') > -1) {
        $("*").each(function () {
            if ($(this).css(key) == rgb) {
                $(this).css(key, '#456780');
            }
        });
    }
}
div {
    height: 50px;
    width: 50px;
}
.common-color {
    color:#789034;
}
.new-cls {
    border-style: solid;
    border-color:#789034;
}
.awesome-one {
    background-color:#789034;
    height:200px;
    width:300px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="common-color">hello</div>
<p class="new-cls">cool</p>
<div class="awesome-one"></div>

EDIT:

Or, if you would rather, here is a simple function to use. Color 1 will be replaced by color 2:

function replaceColor(color1, color2) {
    var keys = Object.keys($('html').get(0).style);
    var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(color1);
    var rgb = 'rgb(' + parseInt(result[1], 16) + ', ' + parseInt(result[2], 16) + ', ' + parseInt(result[3], 16) + ')';
    for (var k in keys) {
        key = keys[k].replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
        if (key.indexOf('color') > -1) {
            $("*").each(function () {
                if ($(this).css(key) == rgb) {
                    $(this).css(key, color2);
                }
            });
        }
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.