MyBB Community Forums

Full Version: how to make optional argument in custom MyCode
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to do this

regex for mycode

\[test=(.*?)\](.*?)\[/test\]


replacement for mycode

<button title="image1" type="button" onclick="if(document.getElementById('spoilerbtn') .style.display=='none') {document.getElementById('spoilerbtn') .style.display=''}else{document.getElementById('spoilerbtn') .style.display='none'}">$1</button>

<div id="spoilerbtn" style="display:none"> $2</div>
as is it works. But i want the [test=???] to be optional. As is it must be there or it does not work. I want it to be optional.

This does not define how to do this
https://community.mybb.com/thread-12008.html

I tried something like this but it doesnt work for the replacement
<?php if ($1 === null){$1 = "Click to expand/condense"}?>
<button title="image1" type="button" onclick="if(document.getElementById('spoilerbtn') .style.display=='none') {document.getElementById('spoilerbtn') .style.display=''}else{document.getElementById('spoilerbtn') .style.display='none'}">$1=defualt</button>

<div id="spoilerbtn" style="display:none"> $2</div>
I add a ? in front of the optional value in the regex...but then the button has no text at all if there is no argument given.
It can be done, but not with one MyCode.

You can create two MyCodes, one for titled spoilers and one for non-title spoilers.

You can use my plugin YourCode to do it because YourCodes can use the eval option, but in MyCode, the eval option isn't available.

Or you can create your own custom plugin and use PHP to decide whether there is a title or not.
Im not educated enough in php to write my own script to detect if there is a title or not.

I tried the plugin yourcode, but to behonest i could not figure out how to do things, when i could with the default mycode. After installing it broke all the regular code tags, and i couldnt figure out how to reinstate them. AT this point ive setup a few custom buttons etc with myinsertbuttons ...im not sure i would want to revert for this one thing as i have done so much modifications with mycode.
(2016-10-31, 01:02 AM)metulburr Wrote: [ -> ]I tried the plugin yourcode, but to behonest i could not figure out how to do things, when i could with the default mycode. After installing it broke all the regular code tags, and i couldnt figure out how to reinstate them.

I won't be home for a few hours, but I'd be happy to help you get YourCode installed and working properly. If there is a bug, I'll try my best to get it fixed.
Thank you, but no thank you. Ive spent the last 2 months tinkering with the forums. I have it close to done. And at this point i am grateful to be done. Adding an optional argument was more of just icing on the cake, not fully required. Im not really keen on switching the whole system of how the BBCode works at the moment. I wish i did that 2 months ago.

Again thank you. Not many plugin authors provide that level of support. I appreciate it.
Then just use two MyCodes. One for titled spoilers and one for standard spoilers.
I see a possible gotcha: you hard-code the ID of the element, so you can only have a single spoiler per page.
thanks for the info. though i am not sure the way around this, i am grateful for second eye.

Is there a way to generate a new id for each new one?
(2016-11-01, 03:11 AM)metulburr Wrote: [ -> ]thanks for the info. though i am not sure the way around this, i am grateful for second eye.

Is there a way to generate a new id for each new one?

No. The answer is to use DOM traversal rather than relying on the id attribute.

<input type="button" onclick="$(this).next('div').hide()">
<div>content</div>