| | |
reset list box is not working javascript
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 27
Reputation:
Solved Threads: 0
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<script type="text/javascript"> function resetLists(){ document.forms["body"].elements["select1"].selectedIndex = 1; document.forms["body"].elements["select2"].selectedIndex = 1; } </script> </head> <!--Start the html body--> <body id="body" onload="isColorSet()"> <form action="" onsubmit="return false;"> <form id="body"> <!--This is where the changes happen--> <div id="test">This is supposed to be a nice little navbar. Select a new font to change this one's</div> </br> </br> <div id="position" style="visibility:visible;position:absolute;bottom:0px; right:0px"> <center> <table width="100%" border ="4" background="raindrop.jpg" > <td> <select name="select1" onchange="newFont(this.value);"> <option value="">Font Family</option> <option value="Arial">Arial</option> <option value="Helvetica">Helvetica</option> <option value="Tahoma">Tahoma</option> <option value="Times New Roman">Times New Roman</option> <option value="Verdana">Verdana</option> </select> </td> <td> <select name="select2" onchange="newFontColor(this.value);"> <option value="">Font Color</option> <option value="Black">Black</option> <option value="Blue">Blue</option> <option value="Green">Green</option> <option value="Orange">Orange</option> <option value="red">Red</option> <option value="Violet">Violet</option> <option value="Yellow">Yellow</option> </select> </td> <td> <button onclick="resetLists()">Reset</button> </td>
here is my code
it works when i test it using a blank listbox with no value
but it is not working with the color value
it says Error:document.forms.body.elements is null or not an object
any idea?
thank you
ayi
•
•
Join Date: Nov 2008
Posts: 27
Reputation:
Solved Threads: 0
sorry
i forget to attach the function to get the value to the listbox
this is the code
try it
i want the reset button set the value back to index 1 for every listbox
but i cannot do that
any suggestion?
thank you
i forget to attach the function to get the value to the listbox
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<html> <head> <script type="text/javascript"> function bakeCookie(name,value){ args=arguments;argc=args.length; expires=(argc>2) ? args[2] : null; path=(argc>3) ? args[3] : null; domain=(argc>4) ? args[4] : null; secure=(argc>5) ? args[5] : false; expDate=new Date();day=24*60*60*1000; if(expires){expDate.setTime(expDate.getTime()+expires*day);} document.cookie=name+"="+escape(value)+ ((expires===null) ? "" : ("; expires="+expDate.toUTCString()))+ ((path===null) ? "" : ("; path="+path))+ ((domain===null) ? "" : ("; domain="+domain))+ ((secure===true) ? "; secure" : ""); } </script> <script type="text/javascript"> function eatCookieVal(name) { endstr=document.cookie.indexOf(";",name); if(endstr===-1) {endstr=document.cookie.length;} return unescape(document.cookie.substring(name,endstr)); } function eatCookie(name) { arg=name+"="; alen=arg.length; clen=document.cookie.length; i=0; while (i<clen) { j=i+alen; if(document.cookie.substring(i,j)===arg){ return eatCookieVal(j); } i=document.cookie.indexOf(" ",i)+1; if(i===0){break;} } } </script> <script type="text/javascript"> function newColor(entry,areaID){ // use DOM method bakeCookie("colorSet",entry,7); // save for a week if (!areaID){areaID="body";} // default to whole body document.getElementById(areaID).style.background=entry; } </script> <script type="text/javascript"> function newFont(entry,areaID){ // use DOM method bakeCookie("fontSet",entry,7); // save for a week if (!areaID){areaID="body";} // default to whole body document.getElementById(areaID).style.fontFamily=entry; } </script> <script type="text/javascript"> function isColorSet(areaID) { // points at color element colorSet=null; if(!(colorSet=eatCookie("colorSet"))){colorSet=null;} if(colorSet!==null){newColor2(colorSet,areaID);} fontSet=null; if(!(fontSet=eatCookie("fontSet"))){fontSet=null;} if(fontSet!==null){newFont(fontSet,areaID);} } </script> <script type="text/javascript"> function resetLists(){ document.forms["body"].elements["select1"].selectedIndex = 1; document.forms["body"].elements["select2"].selectedIndex = 1; } </script> </head> <!--Start the html body--> <body id="body" onload="isColorSet()"> <form action="" onsubmit="return false;"> <select name="select1" onchange="newColor(this.value);"> <option value="">Background Color</option> <option value="Blue">Blue</option> <option value="Green">Green</option> <option value="Orange">Orange</option> <option value="Red">Red</option> <option value="Violet">Violet</option> <option value="Yellow">Yellow</option> </select> <select name="select2" onchange="newFont(this.value);"> <option value="">Font Family</option> <option value="Arial">Arial</option> <option value="Helvetica">Helvetica</option> <option value="Tahoma">Tahoma</option> <option value="Times New Roman">Times New Roman</option> <option value="Verdana">Verdana</option> </select> </br> </br> BLA BLA BLA BLA BLABLA BLA BLA BLA </form> <button onclick="resetLists()">Reset</button> </body> </html>
this is the code
try it
i want the reset button set the value back to index 1 for every listbox
but i cannot do that
any suggestion?
thank you
This will do it...
javascript Syntax (Toggle Plain Text)
<script type="text/javascript"> function resetLists(){ document.forms[0].select1.selectedIndex = 0; document.forms[0].select2.selectedIndex = 0; } </script>
Dev.Opera — FOLLOW THE STANDARDS, BREAK THE RULES...
•
•
Join Date: Nov 2008
Posts: 27
Reputation:
Solved Threads: 0
your code is just reset the listbox but not reset the background color, font type and font size to default
your code and this code do the same thing
just focused the listbox to the selected index
but not change the background color back to the default color
well, i still trying to do it now
but any help would be great
thank you
ayi
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
function resetLists(){ document.forms[0].select1.selectedIndex = 1; document.forms[0].select2.selectedIndex = 1; }
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
function resetLists(){ document.getElementById('select1').selectedIndex = 1; document.getElementById('select2').selectedIndex = 1; document.getElementById('select3').selectedIndex = 1; document.getElementById('select4').selectedIndex = 1; }
your code and this code do the same thing
just focused the listbox to the selected index
but not change the background color back to the default color
well, i still trying to do it now
but any help would be great
thank you
ayi
Sorry, but you didnt mention anything about changing the background back to normal.
I've just based it on the code you've provided and fixed it...
Ok this one should do it...
I've just based it on the code you've provided and fixed it...
Ok this one should do it...
javascript Syntax (Toggle Plain Text)
<script type="text/javascript"> function resetLists(){ document.forms[0].select1.selectedIndex = 0; document.forms[0].select2.selectedIndex = 0; if ((document.forms[0].select1.selectedIndex == 0) && (document.forms[0].select2.selectedIndex == 0)) { document.body.style.backgroundColor = '#FFF'; }; } </script>
We dont actually need to save any values in the cookie, since we know that we are dealing with theRESET button. So we definitely want to clear everything what we have and have it cleaned!
Here's the code and ive fixed it and removed unnecessary script blocks.
Here's the code and ive fixed it and removed unnecessary script blocks.
javascript Syntax (Toggle Plain Text)
<script type="text/javascript"> function bakeCookie(name,value){ args=arguments;argc=args.length; expires=(argc>2) ? args[2] : null; path=(argc>3) ? args[3] : null; domain=(argc>4) ? args[4] : null; secure=(argc>5) ? args[5] : false; expDate=new Date();day=24*60*60*1000; if(expires){expDate.setTime(expDate.getTime()+expires*day);} document.cookie=name+"="+escape(value)+ ((expires===null) ? "" : ("; expires="+expDate.toUTCString()))+ ((path===null) ? "" : ("; path="+path))+ ((domain===null) ? "" : ("; domain="+domain))+ ((secure===true) ? "; secure" : ""); } function eatCookieVal(name) { endstr=document.cookie.indexOf(";",name); if(endstr===-1) {endstr=document.cookie.length;} return unescape(document.cookie.substring(name,endstr)); } function eatCookie(name) { arg=name+"="; alen=arg.length; clen=document.cookie.length; i=0; while (i<clen) { j=i+alen; if(document.cookie.substring(i,j)===arg){ return eatCookieVal(j); } i=document.cookie.indexOf(" ",i)+1; if(i===0){break;} } } /* Additional function added for deleting cookies */ function deleteCookie(name) { document.cookie = name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT' + '; path='; } function newColor(entry,areaID){ // use DOM method bakeCookie("colorSet",entry,7); // save for a week if (!areaID){areaID="body";} // default to whole body document.getElementById(areaID).style.background=entry; } function newFont(entry,areaID){ // use DOM method bakeCookie("fontSet",entry,7); // save for a week if (!areaID){areaID="body";} // default to whole body document.getElementById(areaID).style.fontFamily=entry; } function isColorSet(areaID) { // points at color element colorSet=null; if(!(colorSet=eatCookie("colorSet"))){colorSet=null;} if(colorSet!==null){newColor2(colorSet,areaID);} fontSet=null; if(!(fontSet=eatCookie("fontSet"))){fontSet=null;} if(fontSet!==null){newFont(fontSet,areaID);} } function resetLists(){ document.forms[0].select1.options[0].selected = true; document.forms[0].select2.options[0].selected = true; if ((document.forms[0].select1.selectedIndex == 0) && (document.forms[0].select2.selectedIndex == 0)) { deleteCookie('colorSet'); deleteCookie('fontSet'); document.body.setAttribute('style','font:normal 80% Arial, sans-serif;background:#fff;color:#000;'); } } </script>
Last edited by essential; Jan 20th, 2009 at 3:45 am.
•
•
Join Date: Nov 2008
Posts: 27
Reputation:
Solved Threads: 0
i made changes in here in the resetLists
the selected index goes to number 1 which is mean arial font and blue background color
when push reset button, the listbox is set to the default but the color and the font is not reset to the default
eg. i choose red background and helvetica font
then i press reset button
the background color and the font should be change into blue background and arial font
but it is not changing, just the listbox that reset to default
the page is still the same
the previous code you gave me
it works it change the background color and font to the default setting
but the problem is that when i refresh the page, the page is not default but it goes to the previous setting
eg. i choose red background and helvetica font
then i press reset button
the background color and the font change into blue background and arial font
but when i refresh the page, the background back to red and helvetica font where it should be blue background and arial
I hope you can understand my english
thank you
sorry for troubling you
it is confusing to understand with my bad english
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
function resetLists(){ document.forms[0].select1.options[1].selected = true; document.forms[0].select2.options[1].selected = true; if ((document.forms[0].select1.selectedIndex == 1) && (document.forms[0].select2.selectedIndex == 1)) { deleteCookie('colorSet'); deleteCookie('fontSet'); document.body.setAttribute('style','font:normal 80% Arial, sans-serif;background:#fff;color:#000;'); } } </script>
the selected index goes to number 1 which is mean arial font and blue background color
when push reset button, the listbox is set to the default but the color and the font is not reset to the default
eg. i choose red background and helvetica font
then i press reset button
the background color and the font should be change into blue background and arial font
but it is not changing, just the listbox that reset to default
the page is still the same
the previous code you gave me
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<script type="text/javascript"> function resetLists(){ document.forms[0].select1.selectedIndex = 0; document.forms[0].select2.selectedIndex = 0; if ((document.forms[0].select1.selectedIndex == 0) && (document.forms[0].select2.selectedIndex == 0)) { document.body.style.backgroundColor = '#FFF'; }; } </script>
it works it change the background color and font to the default setting
but the problem is that when i refresh the page, the page is not default but it goes to the previous setting
eg. i choose red background and helvetica font
then i press reset button
the background color and the font change into blue background and arial font
but when i refresh the page, the background back to red and helvetica font where it should be blue background and arial
I hope you can understand my english
thank you
sorry for troubling you
it is confusing to understand with my bad english
![]() |
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: need to include a listbox from one html page to another
- Next Thread: Geolocation API
Views: 2591 | Replies: 13
| Thread Tools | Search this Thread |
Tag cloud for JavaScript / DHTML / AJAX
acid2 ajax ajaxcode ajaxhelp animate array automatically autoplay beta boarder box bug button calendar captcha card cart codes column cookies createrange() css cursor date debugger decimal design developer dom download dropdown element enter error events firefox firehose flash focus form frameworks getselection google gwt html htmlform iframe image() index java javascript javascripts jawascriptruntimeerror jquery jsp listbox maps marquee masterpage menu microsoft mimic mp3 mp4 offline onmouseover parameters php player post problem programming progressbar prototype rating redirect regex safari scale scriptlets search select size sources sql starrating text textarea toggle twitter validation variables w3c web website window windowofwords windowsxp xml xspf






