Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 21st, 2004, 01:00 PM
Friend of Wrox
 
Join Date: Apr 2004
Location: Wausau, WI, USA.
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dynamically set selected attribute

Can anyone point me in the write direction to dynamically set the selected attribute. I am hitting a database and want the value to default to what is the value on the record. The select field is dynamically generated from the following code...

    function setOptions(chosen)
    {
        var selbox = document.frmUpdateRecord.subapplication;

        selbox.options.length = 0;
        if (chosen == " ")
        {
            selbox.options[selbox.options.length] = new Option('Please select one of the Applications above first',' ');
        }
        if (chosen == "Inventory Control & Management Applications")
        {
            <cfloop query="rsInventorysubapp">
            selbox.options[selbox.options.length] = new Option('<cfoutput>#subapplication#</cfoutput>','<cfoutput>#subapplication#</cfoutput>');
            </cfloop>
        }
        if (chosen == "Point-of-Sale & Ordering Applications")
        {
            <cfloop query="rspossubapp">
            selbox.options[selbox.options.length] = new Option('<cfoutput>#subapplication#</cfoutput>','<cfoutput>#subapplication#</cfoutput>');
            </cfloop>
        }

The above is a sample of the code. Thought it might be too long to post. Basically, I am coding using js and cf. I want to dynamically in my js if statement set the selected attribute to what the record says. I can get the info from the db using cf. My problem is that I am unclear as to the syntax to tell it to set it to that value. Something like...

selbox.options.selected = #mycfvalue#

Let me know your thoughts.


Clay Hess
__________________
Clay Hess
Reply With Quote
  #2 (permalink)  
Old December 21st, 2004, 07:35 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: High Wycombe, UK, United Kingdom.
Posts: 344
Thanks: 0
Thanked 1 Time in 1 Post
Default

just invert the code from http://www.jguru.com/faq/view.jsp?EID=818956 and it should do what you need.
Reply With Quote
  #3 (permalink)  
Old December 22nd, 2004, 04:19 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You need to set the selected property of the option to true.
http://msdn.microsoft.com/library/de...s/selected.asp


--

Joe (Microsoft MVP - XML)
Reply With Quote
  #4 (permalink)  
Old December 23rd, 2004, 07:19 PM
Friend of Wrox
 
Join Date: Apr 2004
Location: Wausau, WI, USA.
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Joe,

Thanks for the assist. I see that I need to set that attribute to true. My problem is that I do not know the correct syntax to do that. Can you show me an example please?

Quote:
quote:Originally posted by joefawcett
 You need to set the selected property of the option to true.
http://msdn.microsoft.com/library/de...s/selected.asp


--

Joe (Microsoft MVP - XML)
Clay Hess
Reply With Quote
  #5 (permalink)  
Old December 24th, 2004, 12:23 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

This function should select the option with the desired value.

Code:
function setSelected(optvalue)
{
 for(var i=0; i<this.options.length; i++)
 {
  if(this.options[i].value == optvalue)
  {
   this.options[i].selected = true;
   return true;
  }
 }
 return false;
}

//call the function
document.forms.your_form.your_selectbox.setSelected("#yourcfvalue#");
If the value is not found in all the options, the function will return false.

hth,

-Snib - http://www.snibworks.com
Where will you be in 100 years?
Reply With Quote
  #6 (permalink)  
Old December 24th, 2004, 04:10 PM
Friend of Wrox
 
Join Date: Apr 2004
Location: Wausau, WI, USA.
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Snib,

Thanks for the assist. Where would I place that function? At the end of my original function which sets the options or outside of that? I placed it there and nothing happens. Can you throw some more assistance my way please?

Thanks you and God Bless,


Quote:
quote:Originally posted by Snib
 This function should select the option with the desired value.

Code:
function setSelected(optvalue)
{
 for(var i=0; i<this.options.length; i++)
 {
  if(this.options[i].value == optvalue)
  {
   this.options[i].selected = true;
   return true;
  }
 }
 return false;
}

//call the function
document.forms.your_form.your_selectbox.setSelected("#yourcfvalue#");
If the value is not found in all the options, the function will return false.

hth,

-Snib - http://www.snibworks.com
Where will you be in 100 years?
Clay Hess
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
change the color of selected text dynamically NEO1976 Flash (all versions) 1 December 18th, 2007 11:11 AM
how to give attribute value dynamically gantait XSLT 6 February 17th, 2007 05:00 AM
How to set selected item in Dropdownlist? MTLedari ASP.NET 2.0 Basics 4 December 27th, 2006 12:53 AM
Set Listbox Selected property DaDeViL VB How-To 7 October 4th, 2006 04:04 PM
Setting Selected attribute of DropDownList in Grid jlrolin ASP.NET 1.0 and 1.1 Professional 0 June 13th, 2006 01:59 PM



All times are GMT -4. The time now is 02:29 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.