-1

I am using JQuery to insert divs into a page but i cant seem to get the quotation marks correctly setup.

For example the code below works fine:

var newDiv_1 = '<div id="event_1"><b>Hello World 01</b></div>'; 
$('#mon_Events').append(newDiv_1);

But when i try to use variable in place of the normal text, i doesnt seem to work:

var eventname = 1;
var newDiv_1 = '<div id="event_' . eventName . '"><b>Hello World 01</b></div>';
$('#mon_Events').append(newDiv_1);

How do i use variable inside this statement?

1
  • 4
    either I'm unaware of javascript operators or you need to change it to + eventName + Commented May 16, 2010 at 3:27

2 Answers 2

2

Your eventName is defined as eventname:

var eventName = 1;
var newDiv_1 = '<div id="event_'+ eventName +'"><b>Hello World 01</b></div>';
$('#mon_Events').append(newDiv_1);
Sign up to request clarification or add additional context in comments.

Comments

1

If you have security concerns with HTML string slinging, you can do this:

var newDiv_1 = $("<div><b>Hello World 01</b></div>").attr('id', 'event_' + eventName);

newDiv_1.appendTo('#mon_Events');

Comments

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.