1

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?

4
  • Could you please given an example of the document into which you want the template to result? Commented Jul 29, 2014 at 17:40
  • I tried making the changes myself and the functions work when they are separate but when both functions are together, there is an error where the 'states' can't be changed manually. I edited the question at the bottom. Commented Jul 29, 2014 at 18:06
  • If $STATE_NEW and $STATE_ASSIGNED are strings, you're not quoting them. Commented Jul 29, 2014 at 18:10
  • They are strings, they worked when I didn't quote them but I went ahead and quoted them anyways since it made sense to. However I'm still getting the same error conflict. Commented Jul 29, 2014 at 18:15

1 Answer 1

2
<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>

The way you translated it was correct. And yes, Perl variables can be called into the javascript function the same way you called on them inline.

The reason your code is conflicting is because your second line of code in the .on('change', '#state_id' code is using a single '=' and I think you meant to use '==' to check if the state is 'NEW'. The way you have it now would just change the owner_sel.selectedIndex to '0' everytime the state_id is changed.

if (this.form.state_id.value == <%= $STATE_NEW %> ){
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.