in Education by
How can I replace a specific text with HTML objects? example: var text = "some text to replace here.... text text text"; var element = $('image').event().something... function ReplaceWithObject(textSource, textToReplace, objectToReplace); So I want to get this: "some text to replace < img src...etc >.... text text text" And I would like manipulate the object element without call again $() method. UPDATE: I solved. thanx @kasdega, i made a new script based in your script, because in your script i can't modify the "element" after replace. This is the script: $(document).ready(function() { var text = "some text to replace here.... text text text"; var element = $('image'); text = text.split('here'); $('.result').append(text[0],element,text[1]); $(element).attr('src','http://bit.ly/mtUXZZ'); $(element).width(100); }); I didnt know that append method accept multiples elements. That is the idea, only need to automate for multiple replacements thanx to all, and here the jsfiddle JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
do a split on the text you want to replace then use the array indexes 0 and 1...something like: function ReplaceWithObject(textSource, textToReplace, objectToReplace) { var strings = textSource.split(textToReplace); if(strings.length >= 2) { return strings[0] + objectToReplace.outerHTML() + strings[1]; } return ""; } UPDATE: I found another SO post Get selected element's outer HTML that pointed me to a tiny jquery plugin that helps here. I believe this jsfiddle has what you want. outerHTML is the tiny jquery plugin I included in the JSFiddle. You can also use replace which will reduce some code: http://jsfiddle.net/kasdega/MxRma/1/ function ReplaceWithObject(textSource, textToReplace, objectToReplace) { return textSource.replace(textToReplace, objectToReplace.outerHTML()); }

Related questions

0 votes
    How can I replace a specific text with HTML objects? example: var text = "some text to replace ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    I'm builtin an web resume-generator to learn how to develop for web. I've made a HTML form which ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    I'm builtin an web resume-generator to learn how to develop for web. I've made a HTML form which ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    I have a background image on top of which there is some text. It is very hard to read the text ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    I have dropdown in my form which has a CSS style defined on pageload and is hidden and the code ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I want to calculate browser height without considering browser address bar and bottom navigation bar height. The ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I want to calculate browser height without considering browser address bar and bottom navigation bar height. The ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 10, 2022 in Education by JackTerrance
0 votes
    Please take a look at the following code... $("#page_info_select_box").live("keypress",function(){ var sele = "domain"; var inp ... :sele},function(data){ //alert(data); for (i=0;i...
asked Feb 21, 2022 in Education by JackTerrance
0 votes
    Is there an 'official' way to get all input elements of a form including those outside of the form ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    Why text inside / list is not affected (color of "item1", "item2" is not changed) by CSS applied ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 1, 2022 in Education by JackTerrance
0 votes
    Why text inside / list is not affected (color of "item1", "item2" is not changed) by CSS applied ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 29, 2022 in Education by JackTerrance
...