java script not working after page reload

I have a java script running in create page that calculate risks depending upon selected options of some select lists (these are the risk questions). All questions are mandatory, so user cannot escape without selecting any option in any those select lists. But the problem is, when someone does not enter value in any select list or in any other mandatory field and submits the page, the code stops working properly. I found the reason is that on page reload global variables are getting their initial values which is causing this. But how to either keep the final values of these variable while page is being reloaded or setting the initial options to all select lists?

Any help will be greatly appreciated.

 

<script type="text/javascript">
var  q1 = document.getElementById('customfield_14254');
var  q2 = document.getElementById('customfield_14255');
var  q3 = document.getElementById('customfield_14256');
var  q4 = document.getElementById('customfield_14257');
var  q5 = document.getElementById('customfield_14258');
var  q6 = document.getElementById('customfield_14259');
var  q7 = document.getElementById('customfield_14260');
AJS.$(document).ready(function() {
    AJS.$("#customfield_14049").attr("readonly", true);
});
q1.options[0].text = ' ';
q2.options[0].text = ' ';
q3.options[0].text = ' ';
q4.options[0].text = ' ';
q5.options[0].text = ' ';
q6.options[0].text = ' ';
q7.options[0].text = ' ';
var q1_sel = -1;
var q2_sel = -1;
var q3_sel = -1;
var q4_sel = -1;
var q5_sel = -1;
var q6_sel = -1;
var q7_sel = -1;
q1.onchange=function()
{
    q1_sel = document.getElementById('customfield_14254').selectedIndex;
    q1_sel = q1_sel -1;
    calc_risk();
}
q2.onchange= function()
{
    q2_sel = document.getElementById('customfield_14255').selectedIndex;
    if(q2_sel==3 || q2_sel==4)
	{
	q2_sel=3;
	}
	else if(q2_sel==5 || q2_sel==6 || q2_sel==7 || q2_sel==8)
	{
	q2_sel=4;
	}
	else if(q2_sel==9 || q2_sel==10)
	{
	q2_sel=5;
	}
	//alert(q2_sel);
	q2_sel = q2_sel -1;
	//alert(q2_sel);
    calc_risk();
}
q3.onchange= function()
{
    q3_sel = document.getElementById('customfield_14256').selectedIndex;
    q3_sel = q3_sel -1;
    calc_risk();
}
q4.onchange= function()
{
    q4_sel = document.getElementById('customfield_14257').selectedIndex;
    q4_sel = q4_sel -1;
    calc_risk();
}
q5.onchange= function()
{
    q5_sel = document.getElementById('customfield_14258').selectedIndex;
    q5_sel = q5_sel -1;
    calc_risk();
}
q6.onchange= function()
{
    q6_sel = document.getElementById('customfield_14259').selectedIndex;
    q6_sel = q6_sel -1;
    calc_risk();
}
q7.onchange= function()
{
    q7_sel = document.getElementById('customfield_14260').selectedIndex;
    q7_sel = q7_sel -1;
	if(q7_sel != -1)
	{
	q7_sel = q7_sel +1;	
	}
	//alert(q7_sel);
    calc_risk();
}
function calc_risk()
{
    var risk_score=0;
    var risk_Sum=0;
    var allQsSum = q1_sel+q2_sel+q3_sel+q4_sel+q5_sel+q6_sel+q7_sel;
    if(allQsSum != -7)
    {
        if(q1_sel>=0)
        {
            risk_Sum = risk_Sum + q1_sel;
        }
        if(q2_sel>=0)
        {
            risk_Sum = risk_Sum + q2_sel;
        }
        if(q3_sel>=0)
        {
            risk_Sum = risk_Sum + q3_sel;
        }
        if(q4_sel>=0)
        {
            risk_Sum = risk_Sum + q4_sel;
        }
        if(q5_sel>=0)
        {
            risk_Sum = risk_Sum + q5_sel;
        }
        if(q6_sel>=0)
        {
            risk_Sum = risk_Sum + q6_sel;
        }
        if(q7_sel>=0)
        {
            risk_Sum = risk_Sum + q7_sel;
        }
    }
    else{
        risk_Sum = -7;
    }
    var risk_Sum = q1_sel+q2_sel+q3_sel+q4_sel+q5_sel+q6_sel+q7_sel;
    //alert("risk_Sum:"+risk_Sum);
    if(risk_Sum>=0 && risk_Sum<=6)
    {
        risk_score=1;
    }
    if(risk_Sum>=7 && risk_Sum<=12)
    {
        risk_score=2;
    }
    if(risk_Sum>=13 && risk_Sum<=18)
    {
        risk_score=3;
    }
    if(risk_Sum>18)
    {
        risk_score=4;
    }
    //alert(risk_score);
    if(risk_score > 0)
    {
        document.getElementById('customfield_14261').value = risk_score.toString();
    }
    
    if(risk_Sum<0) {
        document.getElementById('customfield_14261').value = '';
    }
}
</script>

1 answer

Hi Indranil,

You probably load the script through AJS.toInit ... but you fail to load the script again when the page's content changes(e.g. page submits). You probably need to add the code on 

JIRA.Events.NEW_CONTENT_ADDED

event. For more info see this guide

Regards,

Peter

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
Julia Dillon
Posted Apr 17, 2018 in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

767 views 2 19
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