2
var abc = new Array('Perth','Auckland');

case '1':
    document.getElementById(q15).options.length = 0;
    for (i = 0; i < abc.length; i++) {
        createOption(document.getElementById(q15), abc[i], abc.[i]);
    }
break;

var opt = document.createElement('option');
    opt.value = value;
    opt.text = text;
ddl.options.add(opt);

The above code outputs the following:

<option value="1">Perth</option>
<option value="2">Auckland</option>

However, I need to add some PHP to calculate if the option is selected when the page loads, so the output should look something like this:

<option value="1" <?php if ($results['abc']==1) echo "selected";?>>Perth</option>
<option value="2" <?php if ($results['abc']==2) echo "selected";?>>Auckland</option>

However, I'm struggling with the Selected option in the Javascript to add this an option and add it to the drop down list.

Any help appreciated.

Thanks,

H.

3
  • Did you provide all of the javascript code, because I tried to read it, and only bits and pieces of it makes sense... :P Commented Aug 19, 2012 at 14:56
  • what do you mean by "struggling"? what is the challenge? Commented Aug 19, 2012 at 15:03
  • The challenge is that when the page loads, if a drop down option has already been selected (saved in a DB) then when the page loads, the option is auto selected. It works fine when I code in the Drop downs manually but I need this to happen when I'm using Javascript to populate and create the Drop Downs. Commented Aug 19, 2012 at 15:20

2 Answers 2

1

Two things,

First,

You can set the selected attribute using JavaScript by doing domelement.setAttribute("selected","selected");

I think that is what your original question is about.

Second,

You can't add PHP code using javascript, because PHP runs at the server before your browser gets the code.

I'd use PHP to store the $results variable into a javascript variable and use that to set selected.

var results = ["<?php echo implode ('","', $results); ?>"]   /*Get the results array from PHP to javascript.*/


var abc = new Array('Perth','Auckland');

case '1':
    document.getElementById(q15).options.length = 0;
    for (i = 0; i < abc.length; i++) {
        createOption(document.getElementById(q15), abc[i], abc.[i]);
    }
break;

var opt = document.createElement('option');
    opt.value = value;
    opt.text = text;
ddl.options.add(opt);
/*add the "selected" attribute to options that results['abc'] == <number>*/
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks - figured that might be the case.
Thanks Kevin - will have a play shortly.
Will do - it'll be a while but left page open to ensure I do.
@Kevin_Johnson - OK just started playing...having problems imploding the results - the value of results is showing "<?php echo implode ('","', $results); ?>"
Are you interpreting this with PHP? What server language is the rest of the page in?
|
0

Where is $results coming from? If it's something you are operating on in the PHP logic during page creation, then I assume you're specifically asking about why the select item isn't properly being selected?

<option value="1" <?php if ($results['abc']==1) echo "selected";?>>Perth</option>

That should actually be setting the selected attribute to the value "selected" (rather than just printing selected word alone). So, it should be:

<option value="1" <?php if ($results['abc']==1) echo "selected=\"selected\"";?>>Perth</option>

3 Comments

I think that @Homer_J is trying to create the attributes using JavaScript.
Correct Kevin, via Javascript.
Sorry for the misunderstanding guys. I'm new here, so what's proper etiquette? (i.e., delete my answer entirely?)

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.