Jump to content

creating object methods


aspnetguy

Recommended Posts

I have created a new <select> object called DropDownList.I have no trouble adding methods to parts of this object like

<html><head><script>	var SelectBox;	onload=	function Page_Load()	{		SelectBox = new DropDownList(document.getElementById('selectBox'));		SelectBox.Items.Add('Choose Option','');		SelectBox.Items.Add('Zero','0');		SelectBox.Items.Add('One','1');		SelectBox.Items.Add('Two','2');		SelectBox.Items.Add('Three','3');		SelectBox.Items.Add('Four','4');		SelectBox.Items[0].Remove();     //THIS LINE CAUSES ERROR	}		//DropDownList object	function DropDownList(obj)	{		//if obj doesn't exist		if(!obj)		{			var obj = document.createElement('select');			document.body.appendChild(obj);		}		//ddl ID		this.ID = obj.id;		//if ddl is empty avoid error		//by adding temp option		if(obj.options.length == 0)obj.options[0] = new Option('','temp');		//ddl selected index		this.SelectedIndex = obj.selectedIndex;		//ddl value of selected index		this.SelectedValue = obj.options[obj.selectedIndex].value;		//ddl text of selected index		this.SelectedText = obj.options[obj.selectedIndex].text;		//Items array		this.Items = obj.options;		//number of Items in ddl		this.Items.Count = obj.options.length;		//clear ddl array or just specified index		this.Items.Clear = 		function(index)		{			if(index == null) obj.options.length = 0;			else obj.options[index] = null;		}				//Value and Text properites		for(var i=0;i<obj.options.length;i++)		{			this.Items[i].Value = obj.options[i].value;			this.Items[i].Text = obj.options[i].text;			this.Items[i] = obj.options[i];			this.Items[i].Remove = function(){obj.options[i] = null}; //ERROR		}		//remove temp option		if(obj.options[0].value == 'temp')obj.options.length = 0;		//add Items to ddl		this.Items.Add = function(txt,val){obj.options[obj.options.length] = new Option(txt,val)}	}</script></head><body><select id="selectBox"></select></body><html>

Link to comment
Share on other sites

That is not what I want to do.I want ot be able to writeSelectBox.Items[2].Remove() and have it delete the option at index 2.it is not letting me add methods to the array

for(...){    this.Items[i].Remove = function(){...}}

this.Items = selectTag.optionsso I want the function to bethis.Items.Remove = function(){selectTag.options = null}Any ideas,I really want ot use the Remove function with Items[]

Link to comment
Share on other sites

I realize that would work but the whole point of creating the new object is to use it for all methods.I could have easily done all the stuff in my code without the new object and did what you did :) .I am looking to make the functionality easier to use and have added some features that don't exist in select, like SelectedValue.

Link to comment
Share on other sites

I realize that would work but the whole point of creating the new object is to use it for all methods.I could have easily done all the stuff in my code without the new object and did what you did  :) .I am looking to make the functionality easier to use and have added some features that don't exist in select, like SelectedValue.

Ok, I think I get it, this will remove it and without errors: :)SelectBox.Items[0] = null;
Link to comment
Share on other sites

That indeed does work however I am still hoping to find a way to attach a Remove() to the individual options of the Items array.I really want to be able to do thisSelectBox.Items[someindex].Remove();I really want to know why I get an erro when attching a method to the Items array

Link to comment
Share on other sites

Aspnet,Maybe I'm just inane--or is it that you can just do this on the line with the error?

this.Items[i].Remove = obj.options[i] = null; //ERROR

Why would you need the function()? :S

Link to comment
Share on other sites

So like:

<select id="mySelect">	<option>sometext</option>	<option>sometext</option></select><script type='text/javascript'>var iSelect = document.getElementById('mySelect');iSelect.options[0].Remove = function (){	alert('I am doing something');}iSelect.options[0].Remove();</script>

?

Link to comment
Share on other sites

Thanks Blue your code does work, but I have discovered that that was not the problem. The problem was adding Items dynamically. Everytime a item is added the Items array needs to be updated.It is proving more difficult than I had hoped so I will have to settle for SelectBox.Items.Remove(index);Thanks everyone

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...