Board index   FAQ   Search  
Register  Login
Board index php forum :: HTML JavaScript

Dynamically add/remove a SELECT item

Javascript coding ..

Moderators: macek, egami, gesf

Dynamically add/remove a SELECT item

Postby AZDJedi » Sat Apr 14, 2012 10:37 am

Hi,

I'm having trouble getting this code working 100%. Any help would be great!

Basically, I want the dropdown to display "Select from the list", but as soon as it gets focus I want that item to be deleted. When they move off the dropdown I want that item to be re-added (assuming they have selected at least item 2 (item 1 is "add new"). Alternatively, if you can suggest a cleaner way to give them an instruction without having a label...

This is what I have so far, but it keeps removing items 0 AND 1 for some reason :S. The Action variable is passed from onblue and onfocus.

Code: Select all
 if (Action==1) {
      if (cbo.options[cbo.selectedIndex].text=="Select from the list")
         cbo.remove(0);
   }else if (Action==0) {
      if (cbo.value==0) {
         cbo.options[0] = new Option('Select from the list?', -1);
         cbo.value=0;
      }
   }
AZDJedi
New php-forum User
New php-forum User
 
Posts: 5
Joined: Fri Mar 16, 2012 9:20 am

Return to JavaScript

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.