1

I am using the following:

var panelTitleElement2 = $('div.panel-default div.panel-heading');
 panelTitleElement2[thisIndex].css("background-color", "rgb(153,0,0)");

I am getting following error:

Object doesn't support property or method 'css'

panelTitleElement2 has 4 divs that fit the description above. I want to only change the background color for a specific div not all of them. Thank you in advance.

2
  • Print panelTitleElement2 and thisIndex to the console and tell us what it says. Commented Jan 28, 2016 at 22:24
  • panelTitleElement2[thisIndex].currentStyle.backgroundColor = "rgb(153,0,0)"; this says in the console "nomodificationallowed". panelTitleElement2[thisIndex] -- panelTitleElement2 undefined. $('div.panel-default div.panel-heading')[0] gives me the div Commented Jan 28, 2016 at 22:31

2 Answers 2

1

It looks like you're not actually dealing with the element as a JQuery object. I.e., you didn't wrap the element you're trying to reference in the '$()'. When I did exactly what you have (still referring to the element by its index, and not using '.eq()', it worked just fine.

try:

$(panelTitleElement2[thisIndex]).css("background-color", "rgb(153,0,0)");

and just to be sure, maybe try hard-coding in the index you want, in lieu of "thisIndex"

Sign up to request clarification or add additional context in comments.

Comments

1

You need eq() to select indexed element in jQuery collection in order to use jQuery methods on it

panelTitleElement2.eq(thisIndex).css("background-color", "rgb(153,0,0)");

When you use $(selector)[someIndex] it returns the actual dom node at that index in the collection

In reality this can be done on the one line

$('div.panel-default div.panel-heading')
         .eq(thisIndex)
         .css("background-color", "rgb(153,0,0)");

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.