RD Ward
RD Ward

Reputation: 6757

Selected value using jQuery, PHP, MySQL

Does anybody know why I cannot specify the default value this way when it is pulling values from MySQL? I am ultimately trying to have it repopulate the dropdown lists with the appropriate $_REQUEST fields, so that editing can be easier.

  $(document).ready(function(){
    $("#region").load('getRecords.php?start=regions');
    });   //jQuery initializations 
...............

<select 
    class    = "region"
    name     = "region"
    onchange = "value = this.value; 
               $('.country').load('getRecords.php?region='+value)
">

    <option>.....Reading database.....</option>
</select>

<script>$('.region').val('Africa');</script> 
...............

This is the kind of info that gets placed

<option value = "">Select One Region-------</option>

<option value="Africa">Africa</option>

<option value="Americas">Americas</option>

<option value="Asia">Asia</option>

<option value="Australasia">Australasia</option>

<option value="Europe">Europe</option>

<option value = "">------or a Sub-Region------</option>

<option value="Alps">Alps</option>

<option value="Amazon">Amazon</option>
//ETC>>>>

I can only get the jQuery .val method to work for very simple setups.

Upvotes: 0

Views: 1338

Answers (4)

Cyber
Cyber

Reputation: 2724

I recommend like below:

$('#region option[value="' + response_var[0].columnname + '"]').prop('selected', true);

Upvotes: 0

Stephane Gosselin
Stephane Gosselin

Reputation: 9148

Not quite sure what you are aiming for but maybe something like this will give you a nudge in the right direction (taken from the top of my head), probably has syntax errors.

$("select.region option[selected]").removeAttr("selected");
$("select.region option[value='Africa']").attr("selected", "selected");

Upvotes: 1

Luke Sneeringer
Luke Sneeringer

Reputation: 9428

You need to have a valid <option value="Africa"> first before you can make that value become the default. <select> tags do not have arbitrary value attributes.

Upvotes: 0

Deepika
Deepika

Reputation: 474

try this:

document.getElementById("region").selectedIndex=0;//put index of the element which you want as default

and give id="region" in select tag

Upvotes: 0

Related Questions