Results 1 to 5 of 5

Thread: Assigning a class to element when slide active

  1. #1
    Join Date
    Mar 2009

    Assigning a class to element when slide active

    Hello! I am stuck...I'm fairly new to javascript and have been searching for examples of this & the results I come across either don't have examples up anymore (domain is for sale, page doesn't exist) or I just can't get my head around the examples so far - I have a slide show that rotates automatically when the page is loaded and it can also be navigated by clicking on images. I'm trying to add a class to the image tags, depending on which slide is currently being shown.

    For example, the html code for the images looks something like this:
    HTML Code:
    <img src="images/clear-small.png"  alt="1" id="slideOne" onclick="DisplaySlides(0)"/>
    <img src="images/clear-small.png"  alt="2" id="slideTwo" onclick="DisplaySlides(1)"/>
    <img src="images/clear-small.png"  alt="3" id="slideThree" onclick="DisplaySlides(2)"/>
    <img src="images/clear-small.png"  alt="4" id="slideFour" onclick="DisplaySlides(3)"/>
    The javascript I have so far, looks like this:
    window.onload = InitTimeInterval;
    var num=0;
    var thisPic=0;
    var slide_num=0;
    var imgArray= new Array(4);
    // format: src, text, headline, 1st link, 2nd link, 3rd link
    imgArray = [  
     'Description or other text that would give visitors information about this feature &amp; make them want to click for more. Or any other text that you might want to put here. It is up to you.',
     'Easter in KC',
     'Link 1A',
     'Link 1B',
     'Link 1C',
     'Link 1D'],
     'Description B or other text that would give visitors information about this feature &amp; make them want to click for more. Or any other text that you might want to put here. It is up to you.',
     'Link 2A',
     'Link 2B',
     'Link 2C',
     'Link 2D'],
     'Description C or other text that would give visitors information about this feature &amp; make them want to click for more. Or any other text that you might want to put here. It is up to you.',
     'Decorated Eggs',
     'Link 3A',
     'Link 3B',
     'Link 3C',
     'Link 3D'],
     'Description D or other text that would give visitors information about this feature &amp; make them want to click for more. Or any other text that you might want to put here. It is up to you.',
     'Link 4A',
     'Link 4B',
     'Link 4C',
     'Link 4D']
    function LimitNumber(value) {
      if (value < 0) { value = imgArray.length - 1; }
      var value = value % imgArray.length;
      return value; 
    function slide(slide_num,Mypic,Mylbl,imgTitle,imgLinkA,imgLinkB,imgLinkC,imgLinkD) {
    var timedNum = 0;
    function TimedImage() {
      timedNum = LimitNumber(timedNum);
    var timedFunc = '';
    function InitTimeInterval() {
      timedFunc = setInterval("TimedImage()",6000);
      document.getElementById("prevLink").onclick = processPrevious;
      document.getElementById("nextLink").onclick = processNext;
     var sNo = 0;
    function DisplaySlides(SNo) {
    var currImg=0;
    function processPrevious() {
    function processNext() {
    function newSlide(direction) {
    	var imgCt = imgArray.length;
    	currImg = currImg + direction;
    	if (currImg < 0) {
    		currImg = imgCt-1;
    	if (currImg == imgCt) {
    		currImg = 0;

    The name of the class that I'm trying to add from my stylesheet is "slide-active". Can somebody please help me? Thanks!!

  2. #2
    Join Date
    Mar 2009
    If all you want is to set the CSS class of an element---

    var x=document.getElementById("element_id");
    var z=document.createAttribute("class"); //literally--class--not the name of the class

  3. #3
    Join Date
    Mar 2009
    I'm pretty new to javascript, so I'm not sure if the code you gave me is what I'm looking for. I don't see how to use that code to add the class attribute if a certain slide is being viewed. I've tried some different if statements, but can't seem to figure it out - unless I'm using the wrong variables. Maybe if I show you what I'm working on, that would help explain what I'm trying to do. I have the slideshow up at http://www.bellafaithdesign.com/newkcparent/. The first slide says "Easter in KC" and there are four dots between the arrows. Right now if you hover over the dots, they change to orange (with css). However, I am trying to add a class (with js) so that the dot is orange while displaying the corresponding slide. Does that make more sense?

  4. #4
    Join Date
    Mar 2009
    This is not wonderful, but you can fit it in---I assume you have two classes of image in your CSS--I
    shall call them bg(for background) and dis(for display). Here is a function that takes as parameters
    (1)the id of the image that is being retired and (2) the id of the new image that is to be displayed

    function changeClass(oldImage_id,newImage_id){
    var old1=document.getElementById(oldImage_id);
    var new1=document.getElementById(newImage_id);
    var x=document.createAttribute("class");
    x.value="bg"; //change bg to the CSS "class" that the images that are not currently displayed have
    var z=document.createAttribute("class");
    z.value="dis"; //change dis to the class that the iamge that is supposed to be displayed is
    Last edited by Tcobb; 04-02-2009 at 06:14 PM. Reason: lack of clarity

  5. #5
    Join Date
    Mar 2009
    Thank you so much! Something you said finally clicked! I was using id instead of class to define the original style, therefore it would always trump the class. I changed the CSS definition to a class and then some of your code you listed mixed with some I already had typed up & voila! My dots that correspond with the current slide are now orange! (http://www.bellafaithdesign.com/newkcparent/) Thank you thank you thank you! I'm so excited to be over that hill...

    It seems I still have one kink to figure out before this slideshow is finalized. If I click on one of the individual dots (pagination-style), then click the previous/next button, it doesn't always go to the immediate previous/next slide. Any ideas?

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.11070 seconds
  • Memory Usage 2,888KB
  • Queries Executed 13 (?)
More Information
Template Usage (34):
  • (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
  • (1)bbcode_code
  • (1)bbcode_html
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (5)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (5)postbit
  • (5)postbit_onlinestatus
  • (5)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 (71):
  • 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
  • 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