Problem when using brackets in a string

Hi

I have the following javscript functions getting trickered onchange on a select-box. It is returning a number, and the example here works fine.
But when I implement it in my system, the name on the select-box is made dynamically and is i.e. name="id_1[220][54]". And using a name like like, the functions will not work.
I have tried to convert it to a string with String().

Is there a way to get by this other than renaming the select-boxes? Maybe another way to get the inout from the selected box?

Thank you very much, I appriciate it.
<script language="javascript">
function get_val(s){
	str = s.indexOf("(");
	end = s.lastIndexOf(")");
	
	myStr = "Not found!";
	if( str>=0 && end>0 && str<end ) {
		myStr = s.substring( str+1, end ).replace(/[^0-9|.|,]*/g, '');
	}
	alert( myStr );
}

function addto_price(ss)
   {  
   var obj= eval("document.cart_multi."+ss);
   ll = String(obj.name);
   var w = document.cart_multi[ll].selectedIndex;
   var selected_text = document.cart_multi[ll].options[w].text;
   get_val(selected_text);
   }
</script>

<SELECT NAME="mylist" onChange="addto_price(this.name)">
<OPTION VALUE="m1">some text1
<OPTION VALUE="m2">some text2
<OPTION VALUE="m3">some text3 (+636,40DKK)
<OPTION VALUE="m4">some text4
<OPTION VALUE="m5">some text5
</SELECT>

Open in new window

LVL 1
mjarnerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
If you have only one selectbox you can use :
obj = document.getElementsByTagName("select")[0]

or if you can put an id :
obj = document.getElementById("myid")

so to get name : obj.name

0
leakim971PluritechnicianCommented:
if you the selectbox is the second one on your page use :
obj = document.getElementsByTagName("select")[1]

the third :
obj = document.getElementsByTagName("select")[2]

...
0
delizekaCommented:
If I did not get it wrong, you do not need to pass "this.name" in order to have selected option element text and deal with selectbox name.

You can pass selectbox object itself by using onchange event. Then you can get option text as the following :
  <script language="javascript">
function get_val(s){
	str = s.indexOf("(");
	end = s.lastIndexOf(")");

	myStr = "Not found!";
	if( str>=0 && end>0 && str<end ) {
		myStr = s.substring( str+1, end ).replace(/[^0-9|.|,]*/g, '');
	}
	alert( myStr );
}

function addto_price(obj) {
   selected_text = obj.options[obj.selectedIndex].text;
   get_val(selected_text);
}
</script>

<SELECT NAME="id_1[220][54]" onChange="addto_price(this)">
<OPTION VALUE="m1">some text1
<OPTION VALUE="m2">some text2
<OPTION VALUE="m3">some text3 (+636,40DKK)
<OPTION VALUE="m4">some text4
<OPTION VALUE="m5">some text5
</SELECT>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mjarnerAuthor Commented:
Thanks to both of you

I got it working by setting the id of the select box to the same as the name and using the following function. The onchange event is then onchange="addto_price(this.id)"
function addto_price(ss)
   {
   var obj= document.getElementById(ss)
   var selected_text = obj.options[obj.selectedIndex].text;
   get_val(selected_text);
   }

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JScript

From novice to tech pro — start learning today.