views:

78

answers:

2

I'm using jQuery to created a linked TOC that appears in a dialog box. The function I wrote to do so finds all the h4's in the page and

  • gives them ids to link to
  • adds some numbering display info
  • clones them
  • turns the clones into lis
  • wraps the inner text in anchor tags
  • finds the anchors adds a click function to the anchors to close the dialog
  • adds titles and hrefs to the anchors so the links point to the original h4s
  • goes back to the lis
  • appends the lis to a ol the in dialog div

However, in IE7, the cloned h4s are not getting turned in lis. Works in FireFox. In IE7, everything happens as it does in FireFox, just that the the .replaceWith() is seemingly ignore... why?

Looks like this:

$('#content h4').each(function(index) {
   index = index + 1;
    $(this)
   .attr('id', 'tutorial_' + index)
   .before(function() {
            return '<div class="how_to">HOW TO<div><span>' + index + '</span></div></div>';
        })
  .clone()
  .replaceWith("<li>" + $(this).text() + "</li>")
  .wrapInner("<a></a>")
  .find('a')
    .click(function(){
        $("#dialog").dialog("close");
    })
    .attr({
        'title': 'jump to ' + $(this).text(),
        'href': '#tutorial_' + index
    })
   .end()
  .appendTo('#dialog ol')
 });

In action at: http://f1shw1ck.com/jquery_sandbox/tutorials.html

+1  A: 

I don't really understand why people love so much cloning... :)

I would do something like

$('#content h4').each(function(index) {
   index = index + 1;
    $(this)
   .attr('id', 'tutorial_' + index)
   .before(function() {
            return '<div class="how_to">HOW TO<div><span>' + index + '</span></div></div>';
        })
  .clone()
  .html("<li>" + $(this).text() + "</li>")
  .wrapInner("<a></a>")
  .find('a')
    .click(function(){
        $("#dialog").dialog("close");
    })
    .attr({
        'title': 'jump to ' + $(this).text(),
        'href': '#tutorial_' + index
    })
   .end()
  .appendTo('#dialog ol')
 });

But that's just me :)

EDIT: After reading "a lot" about replaceWith() - there seems to be unresolved bug with IE7 & IE6 since... forever. So I discarded my function, took yours and replaced replaceWith with html - which works in this case the way you want, i.e. replace item's html and return itself.

As for replaceWith, you probably ran into one of web vs. IE7 bugs ;)

Adam Kiss
Doesn't work. Look at `.add("<li>" + $(this).text() + "</li>")` -- `$(this)` refers to `$('#dialog ol')` in what you wrote. There is no text there. But I see what you are trying to do. I can get the text and put it in a variable and put that between the `<li>`. That's not my question though, I want to know why what I wrote doesn't work. Is it a bug or not, etc...
two7s_clash
well, i just tried to show you different approach, when you'll have enough of yours :) And I also edited code.
Adam Kiss
I also see you put that code on - there is also mistake - after $(content ol), there are two fullstops (dots) - should be one - already edited here.
Adam Kiss
Thanks for the new tack. I would like to see why what I did doesn't work though...
two7s_clash
edited code again :)
Adam Kiss
Hmm. Going to assume its a bug. Thanks for suggesting the alternate route.
two7s_clash
A: 

There are some typo in that page script

http://f1shw1ck.com/jquery_sandbox/tutorials.html

line 43:

$('#dialog ol').

you have to delete that dot

line 61:

I dont think that }); should be there!

Raspo
Oops, that was me trying Adam Kiss' solution. He corrected. I restored to my original code, which works, except the replaceWith() is ignored in IE7, like I first said.
two7s_clash