0

Html code:

    <h1><a>Minimalist Website</a></h1>
    <h1><a>Fast Food</a></h1>

Javascript code:

var colors = ['#000','#111', '#222']; 
var random_color = colors[Math.floor(Math.random() * colors.length)];
document.querySelector('#menu h1 a:hover').style.cssText= 'text-shadow: random_color + 0px 0px 1px, + random_color + 0px 0px 1px';

What I am trying to do is:

(1) generate a colour from the array.

(2) put that colour into the text-shadow.

What's happening is that the random_colour value taken from the array is not showing up. I've looked at the css and there is no sign of the text-shadow applied.

6
  • And what's not working for you? Commented Apr 11, 2017 at 11:35
  • 1
    As far as I know you can't catch pseudo classes with selectors. Commented Apr 11, 2017 at 11:36
  • random_color is in the string Commented Apr 11, 2017 at 11:36
  • 1
    @Kinduser two issues, 1) he needs to use mouseover event, 2) he needs to loop the array of elements and attach that event to each one of them Commented Apr 11, 2017 at 11:37
  • 2
    @Kinduser someone answered already.. anyways.. Sean Poh -> with events jsfiddle.net/z4dzpqc7 Commented Apr 11, 2017 at 11:47

2 Answers 2

1

Your selector is wrong, in addition to a few other things:

  1. You are looking for #menu which I can't see in the HTML.
  2. You need to use querySelectorAll, to get multiple elements.
  3. Your selector also looks for :hover, but hover is a state of something and not something you can select upon as a query to the DOM.

Try this code:

var colors = ['red','green', 'blue']; 
var random_color = colors[Math.floor(Math.random() * colors.length)];
var items = document.querySelectorAll('h1 a');

items.forEach(item => {
    item.style.cssText = 'text-shadow: ' + random_color + ' 10px 10px 0px';
});

I changed a few things to make it clearer, but hopefully you can adapt it to your needs.

You can play with it here: https://jsfiddle.net/fy0893mm/

Additionally, if you wanted each item to be its own random colour, simply move the random variable into the loop of items.

var colors = ['red','green', 'blue']; 
var items = document.querySelectorAll('h1 a');

items.forEach(item => {
    var random_color = colors[Math.floor(Math.random() * colors.length)];
    item.style.cssText = 'text-shadow: ' + random_color + ' 10px 10px 0px';
});

Also, because you were trying to select :hover, it made me wonder if you wanted this to occur on hover of the element, if so, try this code which will attach the appropriate event listeners to each item.

var colors = ['red','green', 'blue', 'yellow', 'pink', 'orange']; 
var random_color = colors[Math.floor(Math.random() * colors.length)];
var items = document.querySelectorAll('h1 a');

items.forEach(item => {
  item.addEventListener('mouseover', function() {
    item.style.cssText = 'text-shadow: ' + random_color + ' 10px 10px 0px';
  });
  item.addEventListener('mouseout', function() {
    item.style.cssText = 'text-shadow: none';
  });
});
Sign up to request clarification or add additional context in comments.

2 Comments

It is better if you use textShadow so that he doesn't replace entire inline style.. also, added events for the same - jsfiddle.net/z4dzpqc7
Wow! Thanks so much for the answer. I just realised that you changed your answer!! Thanks so much!! I'll keep in mind that you can't really select css psuedo elements. Thanks so much Michael!
0
document.querySelector('#menu h1 a:hover').style.cssText= 'text-shadow: ' + random_color + '0px 0px 1px, ' + random_color + '0px 0px 1px';

3 Comments

(missing space before 0?)
Also add explanation as to what and why have you changed
That's great but I think as @Kind user said, you can't catch pseudo classes with selectors apparently.

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.