Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 21st, 2004, 01:00 PM
Friend of Wrox
 
Join Date: Apr 2004
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
 
Old December 21st, 2004, 07:35 PM
Friend of Wrox
 
Join Date: Jun 2003
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.
 
Old December 22nd, 2004, 04:19 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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)
 
Old December 23rd, 2004, 07:19 PM
Friend of Wrox
 
Join Date: Apr 2004
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
 
Old December 24th, 2004, 12:23 PM
Friend of Wrox
 
Join Date: Nov 2003
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?
 
Old December 24th, 2004, 04:10 PM
Friend of Wrox
 
Join Date: Apr 2004
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





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





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