Leftorro
Leftorro

Reputation: 31

Change content of a div which has a non unique class

Want to change HTML content of a div which has a certain class but problem is there are several others and they get change too.

<div class="x"></div>
<div id="y"><div class"x">Change this</div></div>
<div class="x"></div>

I'm trying

    function ReplaceContentInContainer(matchClass,content)
    {
    var elems = document.getElementsByTagName('*'),i;
    for (i in elems)
        {
        if((" "+elems[i].className+" ").indexOf(" "+matchClass+" ") > -1)
            {
            elems[i].innerHTML = content;
            }
        }
    }

window.onload = function ()
    {
    ReplaceContentInContainer("x","Success"}

but as you can guess it changes every class"x", so how can I limit this only to the class="x" which is nested in a div with id="y"?

Upvotes: 0

Views: 132

Answers (1)

Zach
Zach

Reputation: 1964

Use jQuery

$("#y .x").html("changed it");

That is all

Upvotes: 3

Related Questions