how to set value for textbox using javascript?

tpham March 19, 2012

Hi,

I currently need to get a copy of selected text in a select list and copy it to a defined textbox. My current code is

<script type="text/javascript">

siteCopy = document.getElementById("customerfield_10200");

site = document.getElementById("customfield_10102");

site.onchange= function()

{

siteCopy.text=site.options[site.selectedIndex].text;

}

</script>

and it seemd doesn't work. Please be advised that siteCopy is a textbox custom field and site is a select list custom field.

Thanks.

3 answers

1 vote
Daz
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 16, 2013

The <textarea> element, as with most HTML form inupt elements, uses the `value` attribute to get and set its text. So you can change your code to this:

var siteCopy = document.getElementById("customerfield_10200");
var site = document.getElementById("customfield_10102");

site.onchange = function() {
  siteCopy.value=site.options[site.selectedIndex].value;
}

and it should do what you want.

1 vote
Joe Clark
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 20, 2012

How are you getting this JavaScript loaded into the page? You probably need to wire it up so that the javascript only executes after the page has finished loading.

You can also use jQuery in JIRA to make selecting DOM elements a bit nicer.

Try something like this:

&lt;script type="text/javascript&gt;
AJS.toInit(function() {
  var siteCopy = AJS.$("#customfield_10200");
  var site = AJS.$("#customfield_10102");

  site.change(function() {
    siteCopy.val(site.val());
  });
});


&lt;/script&gt;

tpham March 25, 2012

Hi Joseph,

This one doesn't work for me, is there any other way which doesn't require AJS?

Thanks,

tpham March 25, 2012

to be honest, I think this line has problem

siteCopy.val(site.val());

once I use it, I lose control to custom field, cannot add any other fields or delete any, I had to log into database and delete it manually.

0 votes
RambanamP
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.
March 26, 2012
&lt;script type="text/javascript"&gt;
var siteCopy = document.getElementById("customerfield_10200");
var site = document.getElementById("customfield_10102");
site.onchange= function(){
    siteCopy.value=site.options[site.selectedIndex].text;
or
$("customerfield_10200").val(site.options[site.selectedIndex].text);
}
&lt;/script&gt;

i hope it should work if it is not let me know which version of jira using

Cheers,

Suggest an answer

Log in or Sign up to answer