Trying to append a div to another using the following function customizefields, but i’m not successful. The console returns no errors so I don’t know where to go from here?
javascript
function customizefields(objNames) {
$('td div').each(function(){
name = $(this).text();
if (name.indexOf(" ") != -1) {
name = name.substring(0, name.indexOf(" "));
}
if (objNames[name]) {
$('<div/>', {
id: 'bio',
text: objNames[name].bio
}).appendTo(this);
}
});
}
var namesToChange = {
'Ben':{'class':'pn_cool','bio':'some text for ben'},
'Andrew':{'class':'pn_bad','bio':'some text for andrew'}
};
setInterval(function(){customizefields(namesToChange)}, 1000);
html
<tr>
<td class="stxt2">
<img src="#" class="pic">
<div class="dtxt2" title="07/10/12 09:40 PM">40 mins ago</div>
<b class="nme pn_usr">Andrew</b>: Ut lacus sapien, pretium ut dictum eget, tempus ac nisi.
</td>
</tr>
<tr>
<td class="stxt">
<img src="#" class="pic">
<div class="dtxt" title="07/10/12 09:30 PM">30 mins ago</div>
<b class="nme pn_usr">Ben</b>: Cras vitae cursus lectus.
</td>
</tr>
<tr>
The results I am trying to achieve
<tr>
<td class="stxt">
<img src="#" class="pic">
<div id="bio">TEXT FROM OBJECT WILL GO HERE</div>
<div class="dtxt" title="07/10/12 09:30 PM">30 mins ago</div>
<b class="nme pn_usr">Ben</b>: Cras vitae cursus lectus.
</td>
</tr>
<tr>
It seems to me you should be looking at the textual content of the sibling
btag, not, as currently, thedivtag.Currently, for example, the value of your
namevar (which, by the way, is global) would be ’40’ and, for the next iteration, ’30’.Presumably you meant