SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    function doesn't work with conditions

    Hi,
    Function doesn't work with conditions , but works without them ,
    I need a condition in the function so i can pause, start a slideShow.
    Please how do i do this.
    Code:
    <div class="pause"><a href="javascript:pause();" style="z-index:15" >Pause</a></div>
    <script type="text/javascript" >
    var paused=0;
    function pause(){
    		var pauseNode=document.getElementsByClassName("pause")[0];
    	if(pauseNode.firstChild.nodeValue=="Pause"){
    		pauseNode.firstChild.nodeValue="Start";
    		that.paused=1;
    		clearInterval(that.loop);
    	}else if(pauseNode.firstChild.nodeValue=="Start"){
    		pauseNode.firstChild.nodeValue="Pause";
    		that.paused=0;
    		that.loop=setInterval(next,1000);
    		}
            alert(that.paused); //outputs 0
    /*Works 
    pauseNode.firstChild.nodeValue=="Pause";
    		pauseNode.firstChild.nodeValue="Start";
    		that.paused=1;
    		clearInterval(that.loop);
    	alert(that.paused); //outputs 1
    */
    }
    </script>

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,688
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    Give the link a unique id, and get that style attribute the mother-loving hell outta there, and into a CSS declaration instead.

    Code css:
    #pause {
        z-index:15
    }

    Code highlight:
    <a id="pause">Pause</a>

    Then, once the body has loaded, attach an onclick event on to the pause link. That's most easily done by placing the script at the end of the body, just before the </body> tag.

    Code javascript:
    function pause() {
        ...
    }
    document.getElementById('pause').onclick = pause;

    If the issue you're facing is more complex than that, then I'm sorry. I'm still distracted by being in a earthquake zone where my home has no power, no water, and no sewage.

    For further assistance, please link to a test page on the web that demonstrates the problem you're facing, so that more effective assistance can be granted.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i've posted different html in my first post.
    the html i was trying is this./with js function for better reading
    Code:
    <a class="pause" href="javascript:pause();" >Pause</a>
    <script type="text/javascript" >
    var paused=0;
    function pause(){
    		var pauseNode=document.getElementsByClassName("pause")[0];
    	if(pauseNode.firstChild.nodeValue=="Pause"){
    		pauseNode.firstChild.nodeValue="Start";
    		that.paused=1;
    		clearInterval(that.loop);
    	}else if(pauseNode.firstChild.nodeValue=="Start"){
    		pauseNode.firstChild.nodeValue="Pause";
    		that.paused=0;
    		that.loop=setInterval(next,1000);
    		}
            alert(that.paused); //outputs 0
    /*Works 
    pauseNode.firstChild.nodeValue=="Pause";
    		pauseNode.firstChild.nodeValue="Start";
    		that.paused=1;
    		clearInterval(that.loop);
    	alert(that.paused); //outputs 1
    */
    }
    </script>
    That html does not work with the first pause() function.
    but placed inside a div element like this.
    Code:
    <div class="pause"><a href="javascript:pause();" >Pause</a></div>
    can be reached in javascript like this.
    Code:
    function pause(){
    	var pauseNode=document.getElementsByClassName("pause")[0];
    	var a=document.getElementsByTagName("a")[1]; // second anchor element
    	if(a.firstChild.nodeValue=="Pause"){
    		a.firstChild.nodeValue="Start";
    		that.paused=1;
    		clearInterval(that.loop);
    	}else if(a.firstChild.nodeValue=="Start"){
    		a.firstChild.nodeValue="Pause";
    		that.paused=0;
    		that.loop=setInterval(next,1000);
    		}
    	}
    and it works.
    Thanks for you help.
    Hope you have power,watter, and sewage soon. (our power supplier stops our power for 3 times in the last 3 days, probably not relatated but it sux.)
    Bye.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •