I currently have a select tag with an inline onChange property like this:
<select id="owner_sel" name="owner"
<% print ' style="display: none"' if(!$user->has_permission($task->cid, $PERMISSION_CHANGE_OWNER)); %>
onChange="if(!this.selectedIndex == 0) {
if(this.form.state_id.value == <%= $STATE_NEW %>) {
this.form.state_id.value = <%= $STATE_ASSIGNED %>
}
} else {
this.form.state_id.value = <%= $STATE_NEW %>
}">
<option></option>
<%
foreach my $h (@sorted_users){
foreach my $uid (keys %{$h}) {
my $selected = ($uid == ($form->{owner} || $task->owner)) ? 'selected="selected"' : '';
print qq{<option value="$uid" $selected>$h->{$uid}</option>};
}
}
%>
</select>
and I wanted to move it into a function like this:
<script>
// code...
</script>
Im pretty newbie with javascript and I also don't know if Perl variables can be called the same way in javascript functions?
edit
<script>
$(document).on('change', '#owner_sel', function() {
if(!this.form.owner_sel.selectedIndex == 0) {
if(this.form.state_id.value == <%= $STATE_NEW %>) {
this.form.state_id.value = <%= $STATE_ASSIGNED %>
}
} else {
this.form.state_id.value = <%= $STATE_NEW %>
}
});
$(document).on('change', '#state_id', function() {
if (this.form.state_id.value = <%= $STATE_NEW %> ){
this.form.owner_sel.selectedIndex = 0;
}
});
</script>
----------Question-----------
I redid my code to something like that. When it is just one function, everything works but when I have both scripts included like the snippet above, there is an error.
I was wondering, in my second function if the state_id = 'NEW' and the owner_sel changes to index '0' does that re-trigger the first function?
$STATE_NEWand$STATE_ASSIGNEDare strings, you're not quoting them.