Am un cod sub forma aceasta
Cod: Selectaţi tot
<div></div>
<div></div>
.....
<div></div>
<form></form>
Cum pot selecta sau rescrie primul div de deasupra formului?
Multumesc!
Cod: Selectaţi tot
<div></div>
<div></div>
.....
<div></div>
<form></form>
Cod: Selectaţi tot
<div>dv1</div>
<div>dv2</div>
.....
<div>dvx</div>
<form>form</form>
<script>
//returns the element with tag $tg1 which is before the element with tag $tg2
function getTg1BeforeTg2(tg1, tg2){
var div_form = document.querySelectorAll(tg1+', '+tg2);
var ix =0;
for(var i=0; i<div_form.length; i++){
if(div_form[i].tagName.toLowerCase()==tg2.toLowerCase()){
ix = i-1;
break;
}
}
return div_form[ix];
}
//get the Div before Form
var div_x = getTg1BeforeTg2('div', 'form');
alert(div_x.innerHTML);
</script>