views:

27

answers:

2

I was wondering if I could declare html as a variable. In other words, something like this:

$(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');

});

And then, using the .insertAfter action, place it into my page at different intervals:

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

This doesn't appear to work, am I close to something though?

+2  A: 

Your quotation mark is misplaced.

This:

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

should be:

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

If you want only double quotes, you can escape them like this:

var BoldButton="<input class=\"BoldButton\" type=\"button\" value=\"bold\" id=\"WelcomeBoldButton\">";
patrick dw
Wow, sorry I had to bother you with that!It actually still isn't working, but lemme check it out again..
BOSS
Actually, `insertAfter()` is probably what he wants. http://api.jquery.com/insertAfter/
Andy E
And this: var BoldButton="<input class="BoldButton" type="button" value="bold" id="WelcomeBoldButton">"; should be var BoldButton='<input class="BoldButton" type="button" value="bold" id="WelcomeBoldButton">';
eskimoblood
@Andy E - Yeah, my brain had a cramp. :o)
patrick dw
@patrick: happens to me all the time :-)
Andy E
Is that because I'm using HTML in my variable, because I can declare string variables with double quotations just fine, but when I did the HTML, it didn't work.
BOSS
@BOSS - Doesn't matter if you use single or double. It is just that if you use only one type, you'll end up terminating the string before you intend. Otherwise, you would need to escape the quotes.
patrick dw
OK, thank you so much
BOSS
@BOSS - You're welcome. :o)
patrick dw
+3  A: 

Your quotes are broken. Use ' within ", " within ', or escape the quotes.

$(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 $button = $("<input class='BoldButton' type='button' value='bold' id='WelcomeBoldButton'>");
  $('#intro').after( $button );
});

$button.insertAfter( $('#intro') ) would also work.

Stefan Kendall