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 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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\">";

5 Comments

Wow, sorry I had to bother you with that! It actually still isn't working, but lemme check it out again..
Actually, insertAfter() is probably what he wants. api.jquery.com/insertAfter
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">';
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 - 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.