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

This widget could not be displayed.
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();
	});
	
});

Joe 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!

This widget could not be displayed.

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
Community showcase
Posted Sep 17, 2018 in Confluence

Why start from scratch? Introducing four new templates for Confluence Cloud

Hi my Community friends!  For those who don't know me, I'm a product marketer on the Confluence Cloud team - nice to meet you! For those of you who do, you know that I've been all up in your Co...

571 views 7 6
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