tags:

views:

87

answers:

3

Hi all,

I've read through some of the other posts and followed their suggested advice but to no avail. My function is called via an onclick event embedded within an element. Everything works fine. The data loads, but my focus event handler does not. "d-category" is a loaded element class. It does have multiple occurrences within the loaded code. However, I tried changing the code to a singular id for each element and that still didn't work. Consider the code:

function loadData(id) {
            $("#entry-body").slideUp();
            $("#entry-head").slideUp();
            $("#edit-entry").load("/edit/", { id:id }, function(){
                $(".d-category").focus(function(){
                            $(this).html('<p>example html</p>');
                                    });
                            });
            $("#edit-entry-head").slideDown();
            $("#edit-entry").slideDown();

        };

I have tried moving the block of code containing the focus event handler to other locations in conjunction with .live() to bind it with the loaded elements, but to no avail. Any ideas?


EDIT

I believe the problem with the code above is that .d-category is only matching the first indexed element? However, I have stumbled upon something that works exactly how I want it to. I will add an answer below.

+1  A: 
patrick dw
+1 for the suggestion that focus is being bound to a non form element.
karim79
I've also tried it with .click, but didn't work. The html I am trying to access is a "div".
ionalchemist
@ionalchemist - So you're saying that the `.d-category` elements are successfully added to the page, but when you change your code above to use `.click` instead of `.focus()`, nothing happens? Is that correct?
patrick dw
@patrick dw - Yes, exactly. I am at the moment evolving my html code to compensate for this, but I'd rather not. :-)
ionalchemist
@ionalchemist - Well, based on what you've told me so far, your code should work. Unless there's something that is causing your javascript to crash, the click handlers should fire. Does the other javascript after the `load()` work? For example, does `$("#edit-entry-head").slideDown();` run?
patrick dw
@patrick dw - Yes, the other code works. I've been pulling my hair out about it. I am currently adding input elements to all of my divs to make them "clickable". Hopefully that'll work.
ionalchemist
@ionalchemist - That is strange. Doing `.click()` should work. Using `.live()` is one solution, but before you do that, you should use `.delegate()`. I would use `.live()` only when absolutely necessary. I'll add an answer to show you what I mean.
patrick dw
@patrick dw - .delegate() is not working for me. Firebug sends an error saying that .delegate is not a function. Strange. I am using jQuery-1.4. I'm going to add what I have partially working thus far above. Thanks for all you help. Much appreciated!
ionalchemist
@ionalchemist - `.delegate()` requires 1.4.2. The element that you call it against, like `#dn-category`, will need to be on the page when you call it. With your code changes, I'm not sure if that is the case.
patrick dw
A: 

Use this instead:

$(".d-category").live('focus', function() {
     $(this).html('<p>example html</p>');
})
Eric
Yeah, I've tried that too...and nada. I've tried adding an element specific id instead of the generic class identifier. Didn't work...I'm thinking perhaps I need to ensure that .html is working at all on my doc, maybe there's an issue with my copy of jQuery?
ionalchemist
Well, cleared that up real quick. It's working fine. How presumptious of me to think it's anything other than my oversight!? :-)
ionalchemist
A: 

Here is what works as I wanted it to:

function loadData(id) {
            $("#entry-body").slideUp();
            $("#entry-head").slideUp();
            $("#edit-entry").load("/edit/", { id:id }, function(){
                $('.d-category').each(function(){
                    $(this).one('click', function(){
                        $(this).insertSelectbox();});});

            });
            $("#edit-entry-head").slideDown();
            $("#edit-entry").slideDown();
            }

$.fn.insertSelectbox = function(){
        var sid = $(this).attr('id');
        var opt1 = $('#' + sid).html();
        $("#" + sid).html('<select name="category" id="cat-selector"><option>'  
        + opt1 + '</option>{% for cat in ctgy %}<option>{{ cat.name }}</option>'
        + '{% endfor %}</select>');}

I'm sure it can be refined a little more...but it works!

ionalchemist