Is there a way to use the jquery functions in batch.js?

It seems that a batch.js file provided by confluence has jQuery functions, but I can't seem to use them. I'm not sure what I'm doing wrong if anything. This is a quick sample test I made, but it doesn't work even though batch.js is is called in every page. We are using Conlfuence 3.5.2.

{html}

<div id="test1">hello world</div>

<br/>

<input type="button" id="hide" value="hide">

<input type="button" id="show" value="show">

<script type="text/javascript">

$(document).ready(function(){

$('#hide').click(function(){

$('#test1').fadeOut();

});

$('#show').click(function(){

$('#test1').fadeIn();

});

});

</script>

{html}

2 answers

1 accepted

2 votes
David Simpson Community Champion Nov 07, 2011

See the Confluence UI Guidelines. The guidelines suggest wrapping all custom jQuery in AJS.toInit() if you require the code to be fired after DOMReady.

So your code would be:

AJS.toInit(function(){

	AJS.$('#hide').click(function(){
		AJS.$('#test1').fadeOut();
	});
	AJS.$('#show').click(function(){
		AJS.$('#test1').fadeIn();
	});
	
});

Joseph Clark Atlassian Team Nov 08, 2011

For bonus points, you can structure your toInit function like this:

(AJS.toInit(function($) {

   // Access AJS.$ by just using '$'
   $("#selector").html("Hello, World");

}))(AJS.$);

and it will save you a bunch of keystrokes not having to prefix '$' with 'AJS.' every single time. :-)

Great trick Joe. Thank you!

0 vote

jquery dollar has been "noConflict"ed to AJS.$, so instead use:

AJS.$(function () {
   // page load...
})

Should my code look like this?
&lt;script type="text/javascript"&gt;
AJS.$(function(){
  $(document).ready(function(){
    $('#hide').click(function(){
      $('#test1').fadeOut();
    });
    $('#show').click(function(){
      $('#test1').fadeIn();
    });
  });
});
&lt;/script&gt;

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Kesha Thillainayagam
Posted Apr 13, 2018 in Confluence

We want to hear how your non-technical teams are using Confluence!

Hi Community! Kesha (kay-sha) from the Confluence marketing team here! Can you share stories with us on how your non-technical (think Marketing, Sales, HR, legal, etc.) teams are using Confluen...

2,919 views 27 12
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you