I have got 2 js functions and in a function show I want to get the index of the current visible element, but it always alerts 0. It doesn't seem to check, if function navigate_right()
has changed the ids of the p elements or not.So how can I modify it so that it runs properly?
<html>
<style type="text/css">
p {
border:1px solid black;
width:100px;
height:30px;
display:none;
}
</style>
<p style="display:block" id="p">some text1</p>
<p>some text2</p>
<p>some text3</p>
<p>some text4</p>
<p>some text5</p>
<input type="button" value="left" onclick="show()" />
<input type="button" value="right" onclick="navigate_right()" id="right" />
<script>
var p = document.getElementsByTagName("p");
function navigate_right() {
for (var i = 1; i < p.length; i++) {
if (p[i - 1].style.display == 'block') {
p[i].style.display = 'block';
p[i - 1].style.display = 'none';
return;
}
}
}
function show() {
var c = document.getElementsByTagName("p");
var t;
for (var i = 0; i < p.length; i++) {
if (c[i].id = "vis") {
t = i;
alert(t);
return;
}
}
}
</script>
</html>
EDIT! there should be alert t;t is for index of a current paragraph visible NOW IT WORKS!I have corrected some silly mistakes. But thanx everyone for help anyway
var p = document.getElementsByTagName("p");
function navigate_right() {
for(var i = 1; i <p.length; i++){
if(p[i-1].style.display == 'block') {
p[i].style.display = 'block';
p[i].id = "vis";
p[i-1].style.display ='none';
p[i-1].removeAttribute("id");
return;
}
}
}
function show(){
var c = document.getElementsByTagName("p");
var t;
for (var i = 0; i < p.length; i++) {
if(c[i].id == "vis") {
t = i;
alert(t);
return;
}
}
}