I use something like this to click an element on a page by condition:
var findElem = function(elems, text) {
for (var i = 0; i < elems.length; i++) {
if (elems[i].textContent == text) {
return elems[i];
} else {
var result = findElem(elems[i].children, text);
if (result != undefined) {
return result;
}
}
}
return;
}
switch (document.getElementById('my_id').value) {
case "1":
findElem(document.documentElement.children, "blabla1").click();
break;
case "2":
findElem(document.documentElement.children, "blabla2").click();
break;
default:
break;
}
I want to add one more condition to this code.
Page I'm working is something like this:
<body id="something">
<div id="something2" class="container">
<div id="header">
<a class="hmm" href="somelink"></a>
<a class="aname" target="_blank" href="anotherlink"></a>
</div>
<div id="anotherthing" class="anotherthing2">
<div class="differentthing " style="left: 2px; ">
<div class="asdafad"></div>
</div>
I have to look if differentthing
has no style
in short I want to do something like this:
switch (document.getElementById('my_id').value) {
case "1":
---if differentthing has no style attrib then---
findElem(document.documentElement.children, "blabla1").click();
break;
so if differentthing
section of page is like this
<div class="differentthing ">
then do findElem.click
thing.
if it's <div class="differentthing " style="left: 2px; ">
then do nothing.
I hope I can describe. How can I do that? And sorry for my bad english.
switch (document.getElementById('my_id').value) {
case "1":
// getting an array of elements with "differentthing" class
var elements = document.getElementsByClassName("differentthing");
// if there is just one and it does not have "style" attribute
if (elements.length == 1 && !elements[0].getAttribute("style")){
// perform some action
findElem(document.documentElement.children, "blabla1").click();
}
break;