Simplifying this working JS please

Discussion in 'JavaScript' started by Kayz, Jan 18, 2011.

  1. #1
    Hi guys i have a working JS for joomla which works fine. I needed to edit it to meet my second variable but i dont know how to include it.

    The script works, just need help to incorporate my own 'variable' into the existing one.

    Below is original JS, the existing variable is 'Caption'. Now i want to add my own custom variable which is 'kayz' which should run along with the original 'Caption' variable.

    Your help would be much appreciated.

    Thank you.

    var JCaption = new Class({
    	initialize: function(selector)
    	{
    		this.selector = selector;
    
    		var images = $$(selector);
    		images.each(function(image){ this.createCaption(image); }, this);
    	},
    
    	createCaption: function(element)
    	{
    		var caption   = document.createTextNode(element.title);
    		var container = document.createElement("div");
    		var text      = document.createElement("p");
    		var width     = element.getAttribute("width");
    		var align     = element.getAttribute("align");
    
    		if(!width) {
    			width = element.width;
    		}
    
    		text.appendChild(caption);
    		element.parentNode.insertBefore(container, element);
    		container.appendChild(element);
    		if ( element.title != "" ) {
    			container.appendChild(text);
    		}
    		container.className   = this.selector.replace('.', '_');
    		container.className   = container.className + " " + align;
    		container.setAttribute("style","float:"+align);
    		container.style.width = width + "px";
    
    	}
    });
    
    document.caption = null
    window.addEvent('load', function() {
      var caption = new JCaption('img.caption')
      document.caption = caption
    });
    Code (markup):
     
    Kayz, Jan 18, 2011 IP
  2. buddyborg

    buddyborg Guest

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I'm not sure I understand fully, but i'm going to reply anyway.
    There is a class here, and the class has a method.
    when the class is created, it goes into an 'each loop' and calls the createCaption() function.
    There is a variable passed to the createCaption() function. It appears as if that variable is an object.
    The object has .title and a .width

    the part where there is : var caption = new JCaption('img.caption')
    the variable of interest is : img.caption
    There is something that needs to be modified in that variable in order for you to pass your own variable.
    Good luck with the script. If you have more info that would be helpful, feel free to post.

    One thing I recall about my Joomla practice days was the pain of modifying code. now i'd prefer to write my own modules for my own framework as needed. it took alot of time initially, but saved alot more time overall.
     
    buddyborg, Jan 19, 2011 IP
    Kayz likes this.
  3. Kayz

    Kayz Active Member

    Messages:
    245
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Ahh bravo buddybord, what you have described is exactly right.

    I was playing with var caption = new JCaption('img.caption') and could not get it to work earlier. With your help i have now added a new line beneath it with var caption = new JCaption('img.mycustomdesign')

    It works just as it should! :) thank you again.
     
    Kayz, Jan 19, 2011 IP
  4. buddyborg

    buddyborg Guest

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    awesome. no problem. :)
     
    buddyborg, Jan 19, 2011 IP