0

i was looking for answer on stackoverflow but didn't found it. i'm creating a color switcher

enter image description here

i have a default path <script src="js/theme/theme-default.js"></script> which is working fine on my website

i also created color switcher code

My HTML

 <a href="#" data-style="theme-blue" title="blue: #3498DB" style="background: #3498DB">
        </a>

My JS

 $('.color-changer a').on('click', function(){
        var style = $(this).attr('data-style');
        $('#themeJS').attr('src', 'js/theme/' + style + '.js');
        return false;
    })

lets assume if i click on the first color, the path would be changed to <script src="js/theme/theme-blue.js"></script>

it's changing the path if i inspect but it's not giving me the effects which i want from theme-blue.js

the js files theme-default.js and theme-blue.js

( theme-default.js )

   $('body').css({
    'background-color':'#eee'
   });

( theme-blue.js )

     $('body').css({
    'background-color':'#3498db'
   });

i want to fix this thing with JS

2
  • 1
    You would have to first undo all the changes that the original did before loading the new. and, instead of changing the src, just append a new script element and delete the old. Commented Nov 23, 2015 at 21:08
  • 1
    Why can't you just change the style you want with js instead of loading another js file? Commented Nov 23, 2015 at 21:10

1 Answer 1

2

Daniel -

As far as I know, you cannot replace runtime javascript by changing the src attribute. But what you are trying to do can be done much easier with CSS. Change the data-style to the color you want to see and use this script instead. See the example below:

$('.color-changer a').on('click', function(){
    var style = $(this).attr('data-style');
        $('body').css('background-color', style);
    })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.