Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

224
Views
I cannot get the value of CHtml::dropDownList with Javascript

I am new to PHP and Javascript and I have an issue with the following problem. Here is my html/php:

<div class="row" style="margin-bottom:10px;">
                <div class="col-md-12">
                  <?php echo CHtml::dropDownList('contact_list','',
                  (array)Driver::contactDropList( Driver::getUserId())
                  ,array(
                  'class'=>"contact_list chosen"
                  ))?>
                </div>
            </div>

I am trying to reset this field when a button Cancel is clicked. By reset I mean to make it with value Select from contacts (it is the first option in the dropdown). Here is and my Javascript code, which is not working properly. It only closes the form, but without refresh of the page the dropdown value is there and it is not changed to the default one:

        $( document ).on( "click", ".close-modal", function() {     
            var elements = $('.contact_list');
            elements.select = 'Select from contacts';
            var id=$(this).data("id");
            $(id).modal('hide');
        });

I also tried to reset the whole form with $("#frm").reset(); method, but there was no desired result, too.

This is the default value, which I want to appear every time when the form is open:

Now, it is appearing the previous value, which the user have chosen.

first

second

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

$('.contact_list option:first-child').attr("selected", "selected");

Try this

You can also try the following code:

$(".contact_list").prop("selectedIndex",0);

Note: This option will select the first option of dropdown. Make sure to make "Select your contact" first option

about 4 years ago · Juan Pablo Isaza Report

0

If you want to see the pre-selected value of $(.contact_list) then first you need to set a variable of pre-selected value:

var contactVal = $('.contact_list').find(":selected").val();

Now after $(id).modal('hide'); you set the pre-selected value:

$('.contact_list option[value=contactVal]').attr('selected','selected');

Let me know if this solves your issue.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!