Ask coding questions

← Back to all posts
sense text in a website and replace it with an image with js
Chewyblockguy (9)

I need the code for javascript to sense text in a website and replace it with an image. @ch1ck3n answered partly (

var searchTerm = 'This is',
    replaceWith = 'This is new',
    updateObj = {};
$("#sheet div.item:contains('" + searchTerm + "')").each(function(){
   // use id to build an update object
    updateObj[this.id.replace('field-', '')] = {
        oldText: searchTerm, 
        newText: replaceWith
    }; // not sure what you are trying to save here
   // manipulate html
   this.innerHTML = this.innerHTML.replace(searchTerm, replaceWith);
});
// after, send ajax data `updateObj

)
which was very helpful but it doesn't work in a way that lets me replace the text with an image. please help me with this. I need it to make a certain addon to the scratchaddons extension. thanks!

Answered by ch1ck3n (2052) [earned 5 cycles]
View Answer
Comments
hotnewtop
ch1ck3n (2052)

how exactly do you want to replace a image?

Chewyblockguy (9)

@ch1ck3n just make the text go away and make an image replace it. preferably in a way I can change its size cuz its emoji sized

ch1ck3n (2052)

@Chewyblockguy

var searchTerm = 'This is',
    replaceWith = '<img src="https://example.com/image.png" style="width:100%">',
    updateObj = {};
$("#sheet div.item:contains('" + searchTerm + "')").each(function(){
   // use id to build an update object
    updateObj[this.id.replace('field-', '')] = {
        oldText: searchTerm, 
        newText: replaceWith
    }; // not sure what you are trying to save here
   // manipulate html
   this.innerHTML = this.innerHTML.replace(searchTerm, replaceWith);
});
// after, send ajax data `updateObj
Chewyblockguy (9)

@ch1ck3n btw wdym by after, send ajax data `updateObj manipulate html and use id to build an update object

ch1ck3n (2052)

@Chewyblockguy can you click the checkmark pls

Chewyblockguy (9)

@ch1ck3n how do I send the ajax data? could you give me that code too? thx

ch1ck3n (2052)

@Chewyblockguy can you clarify "send ajax data"

Chewyblockguy (9)

@ch1ck3n @ch1ck3n basicly can you tell me how I an make it update and stuff.