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?
 
delizekaConnect With a Mentor Commented:
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
 
leakim971Connect With a Mentor PluritechnicianCommented:
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.