1

I need to get the javascript code below to work with any of the following links:

<a href="#" class="example1">http://example.com</a>
<a href="#" class="test">Doesnt work now</a>

The code that is displayed after the link is clicked:

<div style='display:none'>
   <div id='example1' style='padding:10px; background:#fff;'>
      <a href="http://example.com" target="_blank">Link</a>
   </div>
</div>

The javascript code I need condensed to work with any class/id values I give:

<script>
    $(document).ready(function(){
        $(".example1").colorbox({width:"50%", inline:true, href:"#example1"});
        $(".example2").colorbox({width:"50%", inline:true, href:"#example2"});
        $(".example3").colorbox({width:"50%", inline:true, href:"#example3"});
        $(".example4").colorbox({width:"50%", inline:true, href:"#example4"});
        $(".example5").colorbox({width:"50%", inline:true, href:"#example5"});
    });

3
  • Where is the code you tried to write? This looks like a please-do-my-homework-for-me question. Commented Jul 29, 2011 at 16:26
  • Are you using a plug-in that allows .colorbox to be used or is there another function not shown? Commented Jul 29, 2011 at 16:27
  • This is working as it is on a current website. I just need to be able to put any value in the link class and match it up to the div id. .colorbox is a plug-in Commented Jul 29, 2011 at 16:30

5 Answers 5

5
$("[class^='example']").each(function() {
    $(this).colorbox({width:"50%", 
                      inline:true, 
                      href:"#example" + $(this).attr("class").replace("example", "")
    });
});

or even simpler:

$("[class^='example']").each(function() {
    $(this).colorbox({width:"50%", 
                      inline:true, 
                      href:"#" + $(this).attr("class")
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0
function myColorBox(myClass, myLink) {
  return $("."+myClass).colorbox({width:"50%", inline:true, href:myLink});
};

Comments

0

It's unclear what you are asking but it sounds like you need a general method to perform the actions listed in your JavaScript snippet. Try something like this:

function doColorBox(sel, href) {
  return $(sel).colorbox({width:'50%', inline:true, href:href});
}
$(document).ready(function() {
  doColorBox('.example1', '#example1');
  doColorBox('.test', 'http://test.com');
  doColorBox('#foo', '#foo');
});

Comments

0

Here's what you're looking for, I believe, http://jsfiddle.net/Skooljester/LggeY/. It gets the a tag's class and then shows a div with the corresponding ID.

Comments

0

A simple for loop might actually be more condensed than some of these clever uses of bits of the jQuery API.

$(document).ready(function(){
  for (var i = 1; i < 6; i++)
    $(".example" + i).colorbox({width:"50%", inline: true, href:"#example" + i});
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.