Adding Attribute to HTML Tag <a href=

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nothing1
    New Member
    • Nov 2007
    • 30

    #1

    Adding Attribute to HTML Tag <a href=

    Ok here is my question i hope its something easy. I have CMS that generates images from whatever url I put with its corresponding syntax. Well I want to add a Lightbox to does images but instead of going into the CMS to find how where it outputs the actual code that appears i was thinking of using JS to add an attribute of rel="lightbox" to links within a certain div that have a class of media.

    Example:
    Here is a link to an image that might be found on my page

    [HTML]<a href="images/blah.jpg" target="_self" class="media">< img src="images/blah.jpg" /></a>[/HTML]

    One way I was thinking of doing it was the function "replace". something like this

    Code:
    javascript:document.body.innerHTML.replace("Index","67890");
    and then this code to find the class which im not sure if it will work.

    Code:
    //Create an array 
    var allPageTags = new Array(); 
    
    function doSomethingWithClasses(theClass) {
    //Populate the array with all the page tags
    var allPageTags=document.getElementsByTagName("*");
    //Cycle through the tags using a for loop
    for (i=0; i<allPageTags.length; i++) {
    //Pick out the tags with our class name
    if (allPageTags[i].className==theClass) {
    //Manipulate this in whatever way you want
    allPageTags[i].style.display='none';
    }
    }
    }
    Can someone please help me put this together. My final link should output as.
    [HTML]<a href="images/blah.jpg" target="_self" class="media" rel="lightbox"> <img src="images/blah.jpg" /></a>[/HTML]
  • nothing1
    New Member
    • Nov 2007
    • 30

    #2
    Looking over the Javascript code for getElementByCla ss it seems like i might not even need the replace function if the getElementByCla ss code work by replacing the "style.display= "none"" to something else right? how do i set a rel?

    Please note that im a total Noob at Javascript im more of a php guy.

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Try:
      [code=javascript]allPageTags[i].setAttribute(" rel","lightbox" );[/code]

      Comment

      • nothing1
        New Member
        • Nov 2007
        • 30

        #4
        thanx! worked perfectly.

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          You're welcome. Glad it worked.

          Comment

          Working...