0

I wrote this code in order to set the text color of an active menu item:

$(document).ready(function () {

var url = window.location.href;
var pieces = url.split("?whatPage=");

$('#pieces[1]').css('color', 'red');
});

If I alert pieces[1] I get the value of the ID of the relevant menu item, but the menu item's color is unaffected. What is most likely to be going wrong?

2
  • you use your variable as your selector-string, you have to concatenate them: $('#' + pieces[1]) Commented Mar 12, 2015 at 10:23
  • thanks, I understand. Changed it, but it doesn't work yet .. Commented Mar 12, 2015 at 10:37

1 Answer 1

3

Instead of $('#pieces[1]').css('color', 'red');

try :

$('#'+pieces[1]).css('color', 'red');
Sign up to request clarification or add additional context in comments.

9 Comments

Hmm tried, but that wasn't it, or at least there is something else incorrect apparently .. Is there perhaps something more fundamentally wrong with my idea, or should it work?
Maybe you don't have the element with that id on the page.
actually I do .. <a id="<?php echo $cols[1];?>menu item</a> I checked page source and it is similar to an alert of pieces[1]
Can you share the HTML and CSS(if any)
so there is no CSS, just the custom one I try to apply to the a-tag.
|

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.