views:

90

answers:

2

I'm using Colorbox to show the html content of hidden divs on my page. I can get this to work perfectly with the following:

$("a.colorbox").colorbox({width:"600px", inline:true, href:"#344"});

This will show the div with the ID of 344.

However, because I'm trying to build a scalable and dynamic page with WordPress, I want to be able to grab the ID of my divs through a function, rather than hard code them in the jquery call.

I modified Jack Moore's example:

$("a[rel='example']").colorbox({title: function(){
    var url = $(this).attr('href');
    return '<a href="'+url+'" target="_blank">Open In New Window</a>';
}}); 

so that it looks like this:

$(".colorbox").colorbox({width:"600px", inline:true, href:function(){
    var elementID = $(this).attr('id');
    return elementID;
}}); 

The problem with this is that the href property of the colorbox function is looking for a string with a # mark infront of the ID. I tried various ways of concatenating the # to the front of the function, including the # in the return value, and concatenating the # to the elementID variable. No luck.

I also tried using the syntax in Jack's example (with no luck) so that my return statement looked like this:

return "#'+elementID+'";

I think my basic question is: How do I use colorbox to show hidden divs on my page without hardcoding everything?

Thanks for your help, Jiert

+1  A: 
return "#" + elementID; 

will have the desired effect as David says.

Liam Bailey
Have a look at: http://stackoverflow.com/editing-help for advice on how to format your answers, so that code *looks* like code, for example. +1, by the way (in a shameless act of self-celebration...) =)
David Thomas
Awesome, thanks folks! That does indeed work. Appreciate it :D
Jiert
Can you share your css/html that went with the jQuery? I'm having a similar problem...what did it look like?
Ash
+1  A: 

I'm facing the same issue. What does your html look like? meaning, how did you structure your "divs"

Mine looks like this: Javascript:

<script>
    $(document).ready(function () {
    $("a.colorbox").colorbox({ width: "50%", inline: true, href: function () {
          var elementID = $(this).attr('id');
          return "#" + elementID;
       } 
      }); 
    });
</script>

And the html looks like (I tried changing the display:none):

<a class='colorbox' href="#">Inline HTML</a>
   <div style="display:none">
       <div id="pop">
          This data is to be displayed in colorbox
       </div>
   </div>
Ash
You need to set the href of your .colorbox link to #pop like:
Liam Bailey
<pre><a class='colorbox' href="#pop">Inline HTML</a> <div style="display:none"> <div id="pop"> This data is to be displayed in colorbox </div> </div></pre>
Liam Bailey