0

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.

6
  • you should really be keeping your JS code separate from your HTML Commented May 18, 2011 at 4:43
  • I agree. But then I thought the sequence might have been the issue, so I put it right underneath. Hah. Commented May 18, 2011 at 4:44
  • You should look into the optgroup property w3schools.com/tags/tag_optgroup.asp Commented May 18, 2011 at 5:20
  • These cascading lists are basically my only option, there are around 4300 provinces, which is the lowest hierarchical level I am aiming for :) Commented May 18, 2011 at 5:24
  • @stefgosselin Alas, read w3fools.com why referencing to w3schools is not advisable. Commented May 18, 2011 at 7:19

4 Answers 4

1

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");
Sign up to request clarification or add additional context in comments.

Comments

0

try this:

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

and give id="region" in select tag

2 Comments

This will only select the first option. The asker wants to set a default value for the select. In this case, it can be any option in the list, provided that option is defined.
Op has jquery goodness available, why the document.getElementById() calls when you have Jquery begging to help you?
0

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.

3 Comments

The values are getting populated via MySQL. Is that not valid option? Along with a lot of other regions.
Where the data is ultimately stored is not the point. The problem is that your markup is wrong. You can get values from MySQL via. an AJAX response or whatever other means you like, but once you have them, you need to create an <option> tag for each, and then select one of those <option> tags to be the default.
I am not sure what you are saying, and I have no idea why you would think I would be pulling options without option tags.
0

I recommend like below:

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

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.