www.webdeveloper.com
Results 1 to 2 of 2

Thread: Please help with DOM scripting.

  1. #1
    Join Date
    Oct 2012
    Posts
    10

    Please help with DOM scripting.

    I am trying to alter some code to use the radio buttons instead of select list to set the number of seconds delay between how fast images change. I am not really sure how to attach the onclick function to these radio buttons for everything to work properly. can anyone please take a look?

    this is the html file:

    Code:
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Slide Show</title>
        <link rel="stylesheet" type="text/css" href="slide_show.css"/>
        <script type="text/javascript" src="slide_show_library.js"></script>
        <script type="text/javascript" src="slide_show.js"></script>
    </head>
    
    <body>
    <div id="content">
        <h1 class="center">Fishing Slide Show</h1>
        <ul class="center" id="imageList">
            <li><a href="casting1.jpg" title="Casting 1">Casting 1</a></li>
            <li><a href="casting2.jpg" title="Casting 2">Casting 2</a></li>
            <li><a href="catchrelease.jpg" title="Catch and Release">
                Catch and Release</a></li>
            <li><a href="fish.jpg" title="Fish">Fish</a></li>
            <li><a href="lures.jpg" title="Lures">Lures</a></li>
        </ul>
        <p class="center">
            <button id="btnPrevious" disabled="disabled">
                <img src="prev.gif" alt="Previous Image" />
            </button>
            <button id="btnPlay">
                <img src="pause.gif" alt="Play or Pause" id="imgPlayPause" />
            </button>
            <input type="button" id="btnSpeed" value="Fast" />
            <button id="btnNext" disabled="disabled">
                <img src="next.gif" alt="Next Image" />
            </button>	
        </p> 
    	<p class="center">
            <label>Set Delay to: </label>
            <form name="selectDelay" id="selectDelay" style="text-align:center">
                
    			<input type="radio" value="1">1 sec
    		    <input type="radio" value="2">2 sec
    		    <input type="radio" value="3">3 sec
    		    <input type="radio" value="4">4 sec
    		    <input type="radio" value="5">5 sec
    		    <input type="radio" value="6">6 sec
    		    <input type="radio" value="7">7 sec
    		    <input type="radio" value="8">8 sec
    		    <input type="radio" value="9">9 sec
    		    <input type="radio" value="10">10 sec
            </form>        
            
        </p>
        <p class="center"><span id="caption">Casting 1</span></p>
        <p class="center">
            <img src="casting1.jpg" alt="Casting 1" id="image" />
        </p>
    </div>
    </body>
    </html>

    and this is the js file:

    Code:
    var $ = function (id) { return document.getElementById(id); }
    
    var SlideShow = function ( params ) {
        var that = this;
    
        // Store references to the element nodes as properties
        if ( !params ) params = {};
        this.listNode = $(params.listId);
        this.imageNode = $(params.imageId);
        this.captionNode = $(params.captionId);
        this.previousNode = $(params.previousId);
        this.playNode = $(params.playId);
        this.playPauseNode = $(params.playPauseId);
        this.nextNode = $(params.nextId);
        this.speedNode = $(params.speedId);
    	this.selectNode = $(params.selectId); ////
    	
    	
    	
    
        // Validate nodes
        this.validateNode( this.listNode, "*", "List ID");
        this.validateNode( this.imageNode, "img", "Image ID");
        this.validateNode( this.captionNode, "span", "Caption ID");
        this.validateNode( this.previousNode, "button", "Previous Button ID");
        this.validateNode( this.playNode, "button", "Play Button ID");
        this.validateNode( this.playPauseNode, "img", "PlayPause Image ID");
        this.validateNode( this.nextNode, "button", "Next Button ID");
        this.validateNode( this.speedNode, "input", "Speed Button ID");
    	this.validateNode (this.selectNode, "form", "User Select ID"); ////  this is where you validate the tags that was used for selct list or box or radio, whatever
    	
    
        // Define application parameters
        this.imageCounter = 0;
        this.play = true;
        this.fast = 1000;   /// 
        this.slow = 10000;  ///
        this.speed = this.fast;
    
        // Retrieve image links
        this.imageLinks = this.listNode.getElementsByTagName("a");
        if ( this.imageLinks.length == 0 ) {
            throw new Error("Slide Show: List ID contains no image links.");
        }
    
        // Process image links
        var i, node, image;
        this.imageCache = [];
        for ( i = 0; i < this.imageLinks.length; i++ ) {
            node = this.imageLinks[i];
    
            // Preload image and copy title properties
            image = new Image();
            image.src = node.href;
            image.title = node.title;
            this.imageCache.push( image );
        }
    
        // Create event handlers
        this.playClick = function () {
            that.togglePlay();
            that.playNode.blur();
        }
        this.previousClick = function () {
            that.displayPrevImage();
            that.previousNode.blur();
        }
        this.nextClick = function () {
            that.displayNextImage();
            that.nextNode.blur();
        }
        this.speedClick = function () {
            that.toggleSpeed();
            that.speedNode.blur();
        }
    	this.changeClick = function () {   ////
    		that.changeSpeed();  ///
    	
    	}  ///
    
        // Attach event handlers
        this.playNode.onclick = this.playClick;
        this.previousNode.onclick = this.previousClick;
        this.nextNode.onclick = this.nextClick;
        this.speedNode.onclick = this.speedClick;
    	this.selectNode.onclick = this.changeClick;   /////
    
        // Set button states
        this.previousNode.disabled = true;
        this.nextNode.disabled = true;
    
        // Start slide show
        this.timer = setInterval(
            function () { that.displayNextImage(); },
            this.speed
        );
    }
    
    SlideShow.prototype.validateNode = function ( node, nodeName, nodeDesc ) {
        if ( ! node ) {
            throw new Error("Slide Show: " + nodeDesc + " not found.");
        }
        if ( node.nodeType !== 1 ) {
            throw new Error("Slide Show: " + nodeDesc +
                " is not an element node.");
        } 
        if ( nodeName != "*" && node.nodeName !== nodeName.toUpperCase() ) {
            throw new Error("Slide Show: " + nodeDesc +
                " is not a " + nodeName.toLowerCase() + " tag.");
        }
    }
    
    SlideShow.prototype.displayImage = function () {
        var image = this.imageCache[this.imageCounter];
        this.imageNode.src = image.src;
        this.captionNode.firstChild.nodeValue = image.title;
    }
    
    SlideShow.prototype.displayNextImage = function () {
        this.imageCounter = ++this.imageCounter % this.imageCache.length;
        this.displayImage();
    }
    
    SlideShow.prototype.displayPrevImage = function () {
        this.imageCounter =
            (this.imageCounter + this.imageCache.length - 1) %
            this.imageCache.length;
        this.displayImage();
    }
    
    SlideShow.prototype.togglePlay = function () {
        var that = this;
        if ( this.play ) {
            clearInterval( this.timer );
            this.playPauseNode.src = "play.gif";
            this.previousNode.disabled = false;
            this.nextNode.disabled = false;
        } 
        else {
            this.timer = setInterval(
                function () { that.displayNextImage(); },
                this.speed
            );
            this.playPauseNode.src = "pause.gif";
            this.previousNode.disabled = true;
            this.nextNode.disabled = true;
        }
        this.play = ! this.play;
    }
    
    SlideShow.prototype.toggleSpeed = function () {
        var that = this;
        if ( this.speedNode.value == "Fast" ) {
            this.speedNode.value = "Slow";
            this.speed = this.slow;
        }
        else {
            this.speedNode.value = "Fast";
            this.speed = this.fast;
        }
    
        if ( this.play ) {
            clearInterval( this.timer );
            this.timer = setInterval(
                function () { that.displayNextImage(); },
                this.speed
            );
        }
    }
    SlideShow.prototype.changeSpeed = function () {  /////
        var that = this;
    		this.fast = this.selectNode.value;  // this takes the value from select list and assigns it to fast button
    		this.fast = this.fast * 1000;
    		this.speed = this.fast;
    		clearInterval( this.timer );
    			this.timer = setInterval(
    				function () { that.displayNextImage(); },
    				this.speed
    			);
    	this.play = true;
    	this.playPauseNode.src = "pause.gif";  ///
    	this.speedNode.value = "Fast";  ///
    	this.speed = this.fast;   ///
    }
    file that creates the object after everything loads

    Code:
    var slideShow;
    
    window.onload = function () {
        var params = {
            listId     : "imageList",
            imageId    : "image",
            captionId  : "caption",
            previousId : "btnPrevious",
            playId     : "btnPlay",
            playPauseId: "imgPlayPause",
            nextId     : "btnNext",
            speedId    : "btnSpeed",
    		selectId   : "selectDelay"
        }
    
        slideShow = new SlideShow( params );
    }
    Last edited by ibex333; 12-06-2013 at 06:49 PM.

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,154
    This, for example...
    HTML Code:
     <input type="radio" value="4">4 sec
    has no onFocus event handler to call the .changeSpeed() function, not had the time to look properly but at a guess you need to
    HTML Code:
     <input type="radio" value="4" onfocue="this.changeSpeed();>4 sec
    and you may need to edit the changeSpeed() function as it looks to me to be written for a select list.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles