I have a string in which I like to replace some part with DOM element:
var mainStr = 'akedf abc dde';
var span = document.createElement('span');
span.id = 'word';
span.innerHTML = '123';
$(span).click(this.touchHandler);
var n = mainStr.replace('abc',span.innerHTML);
var htmlObject = document.createElement('div');
htmlObject.innerHTML = n;
touchHandler:function(e){
console.log('log something')
},
this code does not work, the 'abc' string is replaced with '123' but the touchHandler does not work.
The code is in Backbone.js
Is there some way that I can do this?
touchHandlerif it's the bit that's not working.span, yet you only usespan.innerHTML, this can't clone the event, so just attach the event to$('span',htmlObject)instead, and useouterHTML, notinnerHTMLdocument.createElement()when jQuery lets you do the same thing with less typing?