spires Posted June 5, 2009 Share Posted June 5, 2009 Hi guys I have a dropdown form, the value of which has 2 values that I want. Each of the values are separated by - How do I use JavaScript to display the second section of the value? Here is my code so far: <script language="javascript"> function selBox1(selectbox) { var w = document.myform.mylist.selectedIndex; document.getElementById("div1").innerHTML = selectbox.value; } </script> </head> <body> <form name="myform" > <select name="mylist" onchange="selBox1(this);" id="mylist"> <option value="Text 1 - Text A">Text 1</option> <option value="Text 2 - Text B">Text 2</option> <option value="Text 3 - Text C">Text 3</option> </select> </form> <br><br> <div id="div1"></div> I want to display 'Text A' in the DIV when 'Text 1' is selected Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/161116-solved-spilt-value-in-2/ Share on other sites More sharing options...
roopurt18 Posted June 5, 2009 Share Posted June 5, 2009 http://www.w3schools.com/jsref/jsref_obj_string.asp Look at the split() function. After that it should be trivial to grab the second array index. Quote Link to comment https://forums.phpfreaks.com/topic/161116-solved-spilt-value-in-2/#findComment-850203 Share on other sites More sharing options...
Psycho Posted June 8, 2009 Share Posted June 8, 2009 For something like this, I would just use substr() with an indexOf(). But, either would work. <html> <head> <script language="javascript"> function selBox1(selectbox) { var selectVal = selectbox[selectbox.selectedIndex].value; var secondVal = selectVal.substr(selectVal.indexOf('-')+1); document.getElementById('div1').innerHTML = secondVal } window.onload = function() { selBox1(document.getElementById('mylist')); } </script> </head> <body> <form name="myform" > <select name="mylist" onchange="selBox1(this);" id="mylist"> <option value="Text 1 - Text A">Text 1</option> <option value="Text 2 - Text B">Text 2</option> <option value="Text 3 - Text C">Text 3</option> </select> </form> <br><br> <div id="div1"></div> <body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/161116-solved-spilt-value-in-2/#findComment-851378 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.