Reputation: 110
How to get the first element by id inside the current div ?? means that when class="uc-current" is gone to other element it gets the first element with id="c" in the new element.
I want to do something like this.
function fani(){document.getElementById('uc-current c').className ='c-back';}
any help?
Upvotes: 0
Views: 136
Reputation: 3669
You should never need to do this. IDs are supposed to be unique, so simply using getElementById should be enough. But I've worked on 3rd party sites that disobeyed this rule. So in case you find yourself in that situation, you can try this:
function firstMatchingID(parentElement, id){
var nodes = parentElement.children ;
var ln = nodes.length ;
for(var i = 0; i<ln ; i++){
if (nodes[i].id == id){
return nodes[i] ;
}
}
}
var parentElement = document.getElementsByClassName("c-back")[0]; // Example only. getElementsByClassName is not supported by all browsers
firstMatchingID(parentElement, "uc-current") ;
If you're using jQuery, you can do this:
$(".c-back>#uc-current:eq(0)");
But just looking at that selector makes me cry a little. I've been scarred for life by having to work with reused IDs too many times. One day, I'm going to snap and start hunting down developers.
Upvotes: 0
Reputation:
You probably want to use class names: <div class="c">...</div>
.
Then you do:
document.querySelector('#uc-current .c').className = 'c-back';
Upvotes: 1