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

BJ Yang November 7, 2011

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
Answer accepted
David at David Simpson Apps
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
November 7, 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
Atlassian Team members are employees working across the company in a wide variety of roles.
November 8, 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. :-)

Martin Dulák October 22, 2017

Great trick Joe. Thank you!

0 votes
JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 7, 2011

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

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

BJ Yang November 7, 2011
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
TAGS
AUG Leaders

Atlassian Community Events