Dizzy Bryan High
Dizzy Bryan High

Reputation: 2061

jquery create new div with using array value as id

hi am trying to create a new div but using the value from an array for its id

var divID = 'borough_'+BoroughTagger.myIdArray[intIndex];
jQuery('<div/>', {  
    id: divID  
}).appendTo('#results');

doesn't seem to work ? any ideas? UPDATE: removed comma from the line id: divID, <div></div> is now created but no id property is created.

From what ive tried so far the following from joes answer works: $('#results').append('');

ie a div is created : <div id="borough_2"></div>

i am performing an ajax call after creating this div and wish to do:

success: function(data){
    // data retrived ok
    var myData = data;
    $("boroughId=" + BoroughTagger.myIdArray[intIndex]).html(myData)
}

using joes method the div is created but i still cant reference it to append the data recived from the ajax request. is there some scope problem perhaps?

Upvotes: 2

Views: 1930

Answers (2)

Andy N
Andy N

Reputation: 1123

When you create the div object it returns reference to itself.

var newDiv = jQuery('', {id: divID}).appendTo('#results');

Upvotes: 0

Joe Green
Joe Green

Reputation: 1777

Try

$('#results').append('<div id="borough_'+ BoroughTagger.myIdArray[intIndex] +'"></div>');

If you want to keep a reference to the ID, not a problem, just do

var divId = 'borough_'+ BoroughTagger.myIdArray[intIndex];
$('#results').append('<div id="'+ divId +'"></div>');

You can then access the new element like so:

$('#'+divId).doSomething();

Upvotes: 4

Related Questions