use of jquery in .vm

Shamith K S August 21, 2011

Can we use jQuery in .vm ?

if yes, how ??

3 answers

1 accepted

0 votes
Answer accepted
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.
August 21, 2011

Did you put it in the "on document ready" function as Igor showed?

Shamith K S August 21, 2011

"on document ready" function ???

volalex August 21, 2011

I will try to explain in a more detailed way.

You need to add this code in your script:

AJS.toInit(function (){

AJS.$("#description").text("JIRA") //or AJS.$("#description").val("JIRA") - if it input field

});

if this not help, then you can provide more info about a vm file, and we will try to help you

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.
August 21, 2011

ie a block like this:

(function(){
...
})();

Meaning, run the code in the block when the page is fully parsed and the DOM is available.

Shamith K S August 21, 2011

Still no effect. I am editing description-edit.vm to add default value. its multi line value , i didnt find multiline character in vm syntax. so using java script. but not able to put value into the description text field..

volalex August 21, 2011

You can try to add HTML of your multilined value such as:

Atlassian<br>JIRA

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.
August 21, 2011

In vm syntax there is no syntax for it, you just do it like

<textarea>
a 
multiline
default
value
</textarea>
Shamith K S August 22, 2011

Hi Jamie,

text area seems to be working. but i already hv a txt area. i want to add txt to it. i dont want to create a new txt area..

Shamith K S August 22, 2011

Thanks a lot Alexy. AJSInit() worked. i had done mistake so it was not working. now it worked gud. solved my prob. thanks again.

thnks for Jamie too, i didnt knw abt this multinline in text area..

1 vote
Igor Sereda [ALM Works]
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.
August 21, 2011

I wouldn't say it's "bad" as Alexy suggest, only a bit inconvenient - you can run into conflict between jQuery's $ and Velocity's $" In order to avoid problems, you need to assign $ to another variable:

<script>

(function(){

var q = AJS.$;

q('someselector');

})();

</script>

Of course if you have a large script, it's better extracted into a separate .js file and included on in the .vm via webResourceManager.

Shamith K S August 21, 2011

Thanks Alexy and Igor for your responses. I want to assign default value to description text field using javascript in vm file.

i tried jQuery("#description").text("Jira");

jQuery("#$description").text("Jira");

tried AJS.$("#description").text("Jira");

but not getting value in text field. Am i missing anythin ?

Igor Sereda [ALM Works]
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.
August 21, 2011

If #description is an input or textarea, you need to use val(), not text().

Shamith K S August 21, 2011

i tried that also. no change

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.
August 21, 2011

Did you put it in the "on document ready" function as Igor showed?

0 votes
volalex August 21, 2011

Using Jquery in vm file is bad. But you can use it like that:

<script type="text/javascript">

id = AJS.$("someselector").attr("id")

</script>

Suggest an answer

Log in or Sign up to answer