Jump to content

[SOLVED] spilt value in 2?


spires

Recommended Posts

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.

 

:)

Link to comment
https://forums.phpfreaks.com/topic/161116-solved-spilt-value-in-2/
Share on other sites

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.