![]() |
![]() OCAU News - Wiki - QuickLinks - Pix - Sponsors |
|
|||||||
| Notices |
|
Sign up for a free OCAU account and this ad will go away! Search our forums with Google: |
![]() |
|
|
Thread Tools |
|
|
#1 |
|
Member
Join Date: Jan 2003
Location: Briz Vegas
Posts: 2,333
|
simple question...
Select HTML Code:
<html> <body> <form action=""> <select name="cars"> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="fiat" selected>Fiat</option> <option value="audi">Audi</option> </select> </form> </body> </html> Why? So that when I recall the page/template I can set the value without having to embed PHP logic into the form. SOLVED HTML Code:
<html> <body> <script type="text/javascript"> function setSelectedValue(formObj, newValue) { if(!formObj) return; var objLength = formObj.length; if(objLength == undefined) { formObj.selected = (formObj.value == newValue.toString()); return; } for(var i = 0; i < objLength; i++) { formObj[i].selected = false; if(formObj[i].value == newValue.toString()) { formObj[i].selected = true; } } } //--> </script> </head> <body> <form id="test" name="test" action="test"> <select name="cars"> <option value="">--- Select One ---</option> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="fiat">Fiat</option> <option value="audi">Audi</option> </select> </form> <script>setSelectedValue(document.forms['test'].elements['cars'], 'saab');</script> </body> </html> Last edited by GumbyNoTalent; 25th February 2008 at 3:58 PM. |
|
|
|
| Join OCAU to remove this ad! |
|
|
#2 |
|
Member
Join Date: Sep 2007
Posts: 36
|
I was just about to reply when I saw "SOLVED"
For those of you playing along at home, setAttribute('attribute','value') is probably easier. |
|
|
|
|
|
#3 |
|
Member
Join Date: Jan 2003
Location: Briz Vegas
Posts: 2,333
|
|
|
|
|
|
|
#4 |
|
Member
Join Date: Sep 2007
Posts: 36
|
I havn't tried this locally so I don't know whether the selected works, but the setAttribute command allows you to set an attribute (duh) of an element with the value in the second paramater.
Say you want to set the text to be red and various other things. Sure you can go: Code:
var mySpan = document.getElementById("someSpan");
mySpan.className="someCssSelector";
Code:
var mySpan = document.getElementById("someSpan");
mySpan.setAttribute("style","font-weight:bold;color:red;text-decoration:underline;");
Code:
<span id="someSpan" style="font-weight:bold;color:red;text-decoration:underline;"> Havn't tried it with selected though, but a very handy expression
|
|
|
|
![]() |
| Bookmarks |
|
Sign up for a free OCAU account and this ad will go away! |
| Thread Tools | |
|
|