How to add a button from Admin API using Javascript SDK?

Hello,

I try to use Ghost Admin API (https://ghost.org/docs/api/v3/javascript/admin/) to post articles. Everything works perfect.
However, it seems I can not add a button from the code if I add the html code with “button” element.

When I pass in html: my_html_content
(I use {source: ‘html’})

If my_html_content html code contains “button” element, I see it automatically changed it to “a” element which makes me cannot create a button from the code.

Apart from that because I want when user click it the button should open a new window, I added target=“_blank”, however it is also removed by Ghost when it converted to the mobiledoc I think?

How can I keep those attributes?

Is there any way I can create a button by using the Javascript SDK?

Thanks in advance.

Hi @wanghan,

i’ll try to retrace your problem.
For the time being.
Here’s a little workaround:

Maybe define your button as an a class element as shown below.
<a class="button" href="https://" target="_blank">I'm a Button</a>

Set "button" according to your css class in screen.css

If you are using ?source=html you can add html card content by using comment wrappers, eg:

<!--kg-card-begin: html-->
<button onclick="alert('clicked!'">
<!--kg-card-end: html-->

If you don’t put html into a html card then the content will be reduced to the basic content/elements that are supported by the rich-text editor.

Thanks very much. The comment wrapper works. :grinning:

Thanks for your help. @Kevin’s suggestions below works. :smile:

1 Like