0

Here is the code I'm using to get the background image of selected div from below code

let now = new Date().getHours() * 100 + new Date().getMinutes();
let times = [1015, 1100, 1145, 1230, 1315, 1700, 1750, 1800];
let ids = ['r1', 'r3', 'r5', 'r7', 'r9', 'r11', 'r13'];
let selected = ids[times.reduce((acc, curr, idx) => now >= curr ? idx : acc)];
if (selected) 
  document.getElementById(selected).parentElement.style.backgroundImage = "url('images/g.gif')";

How can I even change the font color of same div which is selected? I tried using this

if (selected) 
  document.getElementById(selected).parentElement.style.backgroundImage = "url('images/g.gif')";
document.getElementById(selected).parentElement.style.color = 'red';

but as I had expected, it didn't work out.

Here is the fiddle https://jsfiddle.net/BlueYeti/yy2nLpg2/3/

Any help will be much appreciated.

3
  • Your code should work, so long as selected gives a valid output and finds an element in the DOM. Could you edit the question to include a working snippet? Although note your question states you want to get the colour, yet you appear to be setting it Commented Aug 10, 2017 at 12:31
  • Getting or setting? You say get but you're setting the colour. Commented Aug 10, 2017 at 12:32
  • Well it was actually about setting. Here is the fiddle, could you please check it. jsfiddle.net/BlueYeti/yy2nLpg2/3 Commented Aug 10, 2017 at 14:24

1 Answer 1

1

Try using jquery something like.

To colour and font-size :-

$( '#id option:selected' ).css({ 'color': 'red', 'font-size': '100%' });

To background image:-

var imageUrl = "images/g.gif";
$( '#id option:selected' ).css('background-image', 'url(' + imageUrl + ')');
Sign up to request clarification or add additional context in comments.

1 Comment

well it is bit complicated as I have different id as r11, r12 and so on . so have no idea where to put all this. Here is the fiddle could you please check it. jsfiddle.net/BlueYeti/yy2nLpg2/3

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.