jquery create new div with using array value as id
hi am trying to create a new div but using the value from开发者_C百科 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?
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();
When you create the div object it returns reference to itself.
var newDiv = jQuery('', {id: divID}).appendTo('#results');
精彩评论