Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help me with explaining this code please

    Hello,

    I want to be a frontend webdeveloper, and I am currently working with the book of o'reilly (html5 + javascript).
    We (the book and I) are building an old television (image) with buttons (play, pause, loop, mute) to interact with a two video files.
    The buttons are functional, so that when you push play, an image is loaded of a play button that is pushed in, pretty neat.
    I am really studying the code and most of it I understand deeply, but now I found some code that I just cant get put my finger on.
    So, to show that I acutally put effort in this code, I supplied the code with my own written comments.

    Code:
    function pushUnpushButtons(idToPush, idArrayToUnpush) {  // We create function with 2 parameters,  push and unpush
    	if (idToPush != "") {  // Lets say we called the function with the button id "play", If play is not empty...
    		var anchor = document.getElementById(idToPush); //id in dom is play
    		var theClass = anchor.getAttribute("class"); //class of play is controls (as I set in the html file)
    		if (!theClass.indexOf("selected") >= 0) { // I get really confused here, If the word selected in theclass is not greater or equal to 0 (?)
    			theClass = theClass + " selected"; //theClass = play selected (?)
    			anchor.setAttribute("class", theClass); //class becomes theClass
    			var newImage = "url(images/" + idToPush + "pressed.png)"; //find the correct image to display that the play button is pushed
    			anchor.style.backgroundImage = newImage; //apply the correct image
    		}
    	}
    //when the unpush button is called --> from pushed to unpushed
    	for (var i = 0; i < idArrayToUnpush.length; i++) { // find the called unpush id in the controls array? (play, pause, loop, mute)
    		anchor = document.getElementById(idArrayToUnpush[i]); // find the matching id in the dom
    		theClass = anchor.getAttribute("class"); // find the matching class in the dom
    		if (theClass.indexOf("selected") >= 0) { 
    			theClass = theClass.replace("selected", ""); // replace selected with nothing, so not selected anymore
    			anchor.setAttribute("class", theClass); //update the class
    			anchor.style.backgroundImage = ""; //update the image
    		}
    	}
    }
    
    function isButtonPushed(id) { // id is called -->  for example mut emute
    	var anchor = document.getElementById(id); // find the id of mute
    	var theClass = anchor.getAttribute("class"); // find the class of id mute
    	return (theClass.indexOf("selected") >= 0); // i dont know
    }

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Quote Originally Posted by sheez44 View Post
    Code:
    		if (!theClass.indexOf("selected") >= 0) { // I get really confused here, If the word selected in theclass is not greater or equal to 0 (?)
    That means "if theClass does not contain 'selected', then...". It should have been written better as if (theClass.indexOf("selected") == -1)

    The indexOf() method returns the index (zero-based) within the calling String object of the first occurrence of the specified value, returns -1 if the value is not found.

  • #3
    New to the CF scene
    Join Date
    May 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv View Post
    That means "if theClass does not contain 'selected', then...". It should have been written better as if (theClass.indexOf("selected") == -1)

    The indexOf() method returns the index (zero-based) within the calling String object of the first occurrence of the specified value, returns -1 if the value is not found.
    Oh ok that makes sense, I didnt know that it would return -1 when not found
    thanks


  •  

    Posting Permissions

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