views:

49

answers:

3

Here's my code:

$(document).ready(function(){

  var Bold="Yes!  There is bold in the text above!!";
  var NoBold="No... There isn't any bolded text in the paragraph above..";
  var BoldButton='<input class="BoldButton" type="button" value="Bold?" id="WelcomeBoldButton">';

  $(BoldButton).insertAfter('#intro');
  $(BoldButton).insertAfter('#LatestNews');

  $(BoldButton).click(function(){
    if($(this).prev().is(':has(b, strong)')){
      alert(Bold);
      } else {
        alert(NoBold);
      }
});

});

For some reason, the alert won't pop up upon clicking the instances of the variables even though the buttons are showing up..

UPDATE:

Here is a working version of the code. I apologize but I'm still very new to JS and jQuery but I don't really understand why this version is working..

$(document).ready(function(){

  var Bold="Yes, some of this text is Bolded!";
  var NoBold="No, none of this text is Bolded..";
  var BoldButton='<input class="BoldButton" type="button" value="Bold?">';
  var MakeMeMoreBoldButton='<input class="MakeMeMoreBoldButton" type="button" value="MakeMe More Bold!">';
  var MakeMeBoldButton='<input class="MakeMeBoldButton" type="button" value="Make Me       Bold!">';

  $(BoldButton).insertAfter('#disclaimer');
  $(BoldButton).appendTo('#navigation');
  $(BoldButton).insertAfter('#intro');

  $('.BoldButton').click(function(){
  if($(this).prev().is(':has(b, strong)')){
  alert(Bold);
  $(this).prev().addClass('BoldText');
  $(MakeMeMoreBoldButton).insertAfter(this).prev();
  $('.MakeMeMoreBoldButton').click(function(){
    $('.BoldText').css('font-weight', 'bold');
  });
  }  
else{
  alert(NoBold);
  $(this).prev().addClass('MakeMeBold');
  $(MakeMeBoldButton).insertAfter(this).prev();
  $('.MakeMeBoldButton').click(function(){
    $('.MakeMeBold').css('font-weight', 'bold');
  });
  }
  });
  });
A: 

Have you tried attatching the click event before inserting the buttons into the DOM.

Ben Robinson
That's worth a try; I don't know whether it'd work.
Pointy
+4  A: 

That's not going to work, as you've demonstrated by empirical testing. What you're doing is creating some DOM content and then copying it a couple times into the document. You need to set up your "click" handlers on the real DOM nodes that are actually in the document.

Pointy
Who downvoted this? It's the correct answer! The OP is adding the click handler to a jQuery wrapper created from a HTML string, not the elements that have been inserted into the DOM.
Andy E
Thanks @Andy E ~~sniff~~~ my poor answer :(
Pointy
How do I set up the click handlers on the real DOM nodes that are actually in the document?
BOSS
+1 Since I can't stand it when people down-vote without giving reason. They should read the faq.
patrick dw
@BOSS you could do what @Stefan suggested, or you could just do `$('button.BoldButton').click(function() { ... })` after you do the "insertAfter" calls.
Pointy
+1  A: 

Move the event handler before the insertions. A better way to do this, however, is this:

  $('.BoldButton').live('click',function{

     if($(this).prev().is(':has(b, strong)')){
       alert(Bold);
     } else {
       alert(NoBold);
     }
 });
Stefan Kendall
If the event handler is set up (with .live() like that) in that function, then those variables will be in scope.
Pointy
OK, I sort of get it, but why is it not in the scope yet? I have it placed first, if that matters.Also, what does .live really do? The jQuery API just kind of confused me on this one.. :)
BOSS
The "live" routine will put an event handler on the `<body>` tag. That handler will be called by any "click" event on the page. When the handler gets an event, it checks to see if it's actual target element matches the selector used to set it up: in this case, ".BoldButton". If it does match, then that handler will call *your* event handler, which will then work pretty much like it would if it had been attached with just "click" (not exactly but close enough for most purposes).
Pointy
You clearly don't understand the purpose of live, then. The point is that this might not be the only place .BoldButton elements get created, and as such this is the **preferred way to add event handlers to dynamically created DOM elements**.
Stefan Kendall
Stefan - I also don't understand why you don't think those variables are in scope. You could actually place your `.live()` *before* the variable declarations inside `.ready()` and it would work.
patrick dw
@Patrick: Righto. I was thinking of C-like variable binding, in which case Bold and NoBold would be out of scope. Ack.
Stefan Kendall