How to retrieve macro parameters of Confluence?
Asked by AustinS:

I've created a user macro for Confluence and I was trying to retrieve macro parameters by using attribution "*data-macro-parameters*" to re-edit this macro and update parameters of this macro.

Now I can retrieved correct parameters when there is only one macro on the Confluence page, BUT if added multiple same ,macros on the same page, then re-edit any of macro except the first one, I always get the first's parameters, that because these macro all has attribution "*data-macro-parameters* ". How could identify which macro user wants to edit?

Any help could greatly appreciated.

These macros have similar html code:

```
<img class="editor-inline-macro" src="/confluence/plugins/servlet/confluence/placeholder/macro?definition=e3Jtc3ZpZXdlci1tYWNybzpQYXJhbWV0ZXJzPXsicHJvamVjdCI6WyJXSElURSJdLCJ0YWciOlsiV0hJVEUxMjggcHVibGljIl0sImZpZWxkcyI6WyJpZCIsIkNhdGVnb3J5IiwiRGVzY3JpcHRpb24iLCJNaW4iLCJUeXAiLCJNYXgiLCJVbml0Il0sIm5vbnBhcmFtZmllbGRzIjpbImlkIiwiQ2F0ZWdvcnkiLCJUeXBlIiwiRGVzY3JpcHRpb24iXX19&amp;locale=en_GB&amp;version=2" data-macro-name="my-macro" data-macro-parameters="Parameters={&quot;project&quot;:[&quot;Woods&quot;],&quot;tag&quot;:[&quot;WHITE128 public&quot;],&quot;fields&quot;:[&quot;id&quot;,&quot;Category&quot;,&quot;Description&quot;,&quot;Min&quot;,&quot;Typ&quot;,&quot;Max&quot;,&quot;Unit&quot;],&quot;nonparamfields&quot;:[&quot;id&quot;,&quot;Category&quot;,&quot;Type&quot;,&quot;Description&quot;]}" data-macro-schema-version="1">
```


Answers:
author: James Richards
body: Hi AustinS,

Confluence has jQuery built in, so you can iterate over the html elements you want with something like

```
AJS.$('img.editor-inline-macro').each(function(index) { console.log( index + ": " + $( this ).text() ); });
```

You can see more about the *each()* function in the jquery docs here

* <https://api.jquery.com/each/>

You may find if you enter that JavaScript into tinymce then it will strip out the code, so better to put it into a HTML macro block.

James.

comments: author: AustinS
body: Thanks James, but what I need is when user randomly click to edit the one macro of this page, can retrieve the parameters of this one, not the first one's.

We do not need to iterate to get each of them, just want to identify which macro is user 'click to edit'. Because for each \<img\>, there is no 'id' attribution to identify them. Any idea ?

Thanks again.

S


author: James Richards
body: Ah, so you mean when the editor is being used you want to get the one that is selected?

Hmmm, that's pretty tricky because you need to insert a click listened in to the DOM and wait for events.

Something like this would do the trick

```
AJS.$('img.editor-inline-macro').on('click', function() { alert('yes!'); });
```

See

* <http://api.jquery.com/on/>

But it doesn't look like the AJS object is defined when using the editor for the editor items.

I'd suggest asking over in the developer community at

* <https://community.developer.atlassian.com>

James.


author: AustinS
body: Thanks for your replies, James.

Will do that.

Happy Thanksgiving!

S


author: AnnWorley
body: Hi! May I know what tool you are using to retrieve the macro parameters?

comments: author: AustinS
body: Im using Tinymce as editor. And using JavaScript to getting parameters.


Tags: developer