Jump to content

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>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.