davepmiller
davepmiller

Reputation: 2708

Highlighting a div element within another div

I'm trying to alter the background color of a child element.

This is attempted with the code:

arrdiv[i].highlight.style.backgroundColor = "black";

Where highlight is the name of the div i'm attempting to alter. This is obviously incorrect, but hopefully it demonstrates what I'm trying to do.

Here is the function:

function mainFormHideShow(id, highlight_id, arrDiv) 
{ 
    var idNum = id*1;
    var l = arrDiv.length;
    var i = idNum%l;
    var highlight = 'highlight' + highlight_id;

    for (var j=0; j<l; j++)
    {
        $(arrDiv[j]).hide();
    }

    arrDiv[i].style.display = "block";

    arrdiv[i].highlight.style.backgroundColor = "black";

}

Upvotes: 0

Views: 214

Answers (1)

MatuDuke
MatuDuke

Reputation: 5008

I would use id property rather than name property. So, you can do this:

document.getElementById('highlight').style.backgroundColor = "black";

If you want to use name property anyway, you have to do something like this:

var myDivs = document.getElementsByName('highlight');
myDivs[0].style.backgroundColor = "black";

If you're using jQuery:

$("#highlight").css("backgroundColor", "black");

Or:

$("[name=highlight]").css("backgroundColor", "black");

Upvotes: 2

Related Questions