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 07:49 PM.

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,874
    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.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.11530 seconds
  • Memory Usage 2,868KB
  • Queries Executed 15 (?)
More Information
Template Usage (34):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (3)bbcode_code
  • (2)bbcode_html
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (2)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (2)postbit
  • (2)postbit_onlinestatus
  • (2)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates