Javascript version of replace & include?

Sugarcube 2.35.0

Hi there!

This code works perfectly in Javascript, but I was just curious if anyone knew what the vanilla javascript method would be for doing this?
$.wiki('<<replace "#randomDivElement">><<include "someOtherPassage">><</replace>>');

Using Story.get I was able to get the text of “someOtherPassage” but I’m also setting variables in that passage and they weren’t working out properly…

Rather than strictly vanilla I assume you meant more purely JavaScript. In which case:

1 Like

ahhhh cool. Is that Jquery?

Could I achieve something similar with something like this?

let el = document.getElementById("randomDivElement");"someOtherPassage").processText());

Sorry for the 20 questions, I’m just trying to understand what’s going on behind these functions :slight_smile:


Not like that, no. SugarCube’s <jQuery>.wiki() method isn’t a DOM API.

You could find the element with DOM APIs and then pass it to jQuery, but there’s not much point in that. Still, as an example:

const el = document.getElementById("randomDivElement");
1 Like

Ahhhh lol thanks. I didn’t realise there was a difference between DOM and not DOM lol. Got a lot to learn.

I think I also finally realised what the wiki example in Sugarcube’s documentation is actually doing.

$('#the-box').wiki('Who //are// you?');

That code grabs the element with id called “the-box” and then appends ‘Who are you?’ into the box. In normal html double “//” doesn’t mean anything, but in Sugarcube it means italic. So this little piece of code is turning it into processed(??) html and also sticking it inside the element?

Yes. The $('#the-box') part selects the matching element, while the wiki('Who //are// you?') part renders the passed text and appends it to the selected element(s).

1 Like

Ok awesome thanks heaps!