views:

1019

answers:

1

How do I make the below function more dynamic for example commentLink and commentContainer will have an ID after them like this commentLink-2289 commentContainer-2289 because there will be multiple ones in a list.

Javascript

$(function() {
    $("#commentLink").click(function() 
   {
        $("#commentContainer").toggle();
        if ($("#commentContainer").is(":visible")) 
        {
            $("#commentContainer").load($(this).attr("href"));
        } else 
        {
             $("#commentContainer").html("Loading..."); //Or just leave it as is...
        }
        return false; //Prevent default action
   });
});

Html

<div id="SystemNews">
 <ul id="dc_news">
   <li>
     <a href="/Home/SystemNews/69" id="commentLink-0">Comments</a>
     <div id='commentContainer-0' style="display:none;">    Loading...</div>
   </div>
   </li>
   </ul> 
</div>

Note: Please provide a working example, I learn best by example thanks

+2  A: 

Try this:

jQuery(function($){ 
  $("a[id^='commentLink-']").click(function(){
    match = this.id.match(/commentLink-(\d+)/);
    container = $("div#commentContainer-"+match[1])
    container.toggle();
    if (container.is(":visible")) {
        container.load($(this).attr("href"));
    } else {
        container.html("Loading...");
    }
    return false; //Prevent default action
  });
});

But It would help if you can post the html or a link to the html so we can see what the format you are working with is. I have no idea if the error you are getting is from my code or yours (since my code is just a mod of yours). Which line number is the error on in the script?


Edit My original code had a bug with the this.id.match line, that's been fixed above, try it now.


Edit : Also, there is an extra closing </div> in your provided HTML, not sure if that's a typo or not, but it should be removed.

The click event should be wrapped in $(document).ready(function(){ ... }); so that jquery can accurately access the dom (since you are querying for <a> and <div> elements). I'm getting a problem where the last return false; is not being reached, I suspect it has something to do with the is(:visible) code. What is the intended function of that if/else block?

localshred
Ack, could you attempt to progressively refine your answers, mutliple answers often get confusing. I'm really tempted to edit your answers so that we never need to realise there was an earlier question, by copy-pasting the sample code everywhere, but I'll leave that up to you to do.
Kent Fredric
Thanks for you help, I actually got it to work finally.
dswatik
Kent, do you mean I need to progressively refine, or dswatik? He asked the question before, then moved it to this one with my proposed answer from the previous question. I've just been trying to get the best answer for him. I Apologize if I was doing things improperly. Thanks for cleaning up. :)
localshred
What I'm saying here is there are 3 answers, all yours, 2 posted by you, and 1 posted by him from the previous question. This makes no sense to me and I figure it would be better to have *one* cohesive answer for the people who *didn't* see the previous question.
Kent Fredric
I would agree 100%, I was hesitant to post follow up comments as comments since I wasn't sure he was reading them. I would appreciate the edit/organization and I'll definitely do better on subsequent questions. Thanks!
localshred
I've cleaned it up a bit, take a look at the result and make sure you're happy with how it is, because I don't want to be saying things *for* you, so you need to agree with my changes ;)
Kent Fredric
That looks like exactly what it should. Thanks again.
localshred