Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Splitting output text within a function 1

Status
Not open for further replies.

hamish75

Technical User
Dec 18, 2009
39
GB
I have a javascript function that foxbox kindly provided me with on another ASP related thread.

Basically i'd like to take this function and add a split within it.

here is the code:

Code:
<script language="javascript">

function FillText() {
       var selObj = document.getElementById('EditStock');
       var txtObj = document.getElementById('EditDescription');

        var selIndex = selObj.selectedIndex;
        var txt = txtObj.value;
        txtObj.value = selObj.options[selIndex].text;
	   
       }
    
</script>

<form method="post" action="edit.asp">
<select name="EditStock" id="EditStock" onchange="FillText();">
<option value="AA">1 One</option>
<option value="BB">2 Two</option>
<option value="CC">3 Three</option>
<option value="DD">4 Four</option>
</select>
<input type="text" id="EditDescription" name="EditDescription" />
</form>

Basically i want to be able to split the value so i only get one, two, three or four in the input box rather than 1 one, 2 two, 3 three or 4 four.

Can anyone help?

Thanks.


Ah dinnae ken...
 
Hi

With abit of additional change : is preferable to pass a reference than to query the whole [tt]document[/tt] for it each time.
JavaScript:
[b]function[/b] [COLOR=darkgoldenrod]FillText[/color][teal]([/teal][highlight]selObj[/highlight][teal])[/teal]
[teal]{[/teal]
  [b]var[/b] txtObj [teal]=[/teal] document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'EditDescription'[/i][/green][teal]);[/teal]

  [b]var[/b] selIndex [teal]=[/teal] selObj[teal].[/teal]selectedIndex[teal];[/teal]
  [b]var[/b] txt [teal]=[/teal] txtObj[teal].[/teal]value[teal];[/teal]
  txtObj[teal].[/teal]value [teal]=[/teal] selObj[teal].[/teal]options[teal][[/teal]selIndex[teal]].[/teal]text[highlight][teal].[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal][fuchsia]/\d+ /[/fuchsia][teal],[/teal][green][i]''[/i][/green][teal])[/teal][/highlight][teal];[/teal]
[teal]}[/teal]
HTML:
[small][b]<form[/b] [maroon]method[/maroon][teal]=[/teal][green][i]"post"[/i][/green] [maroon]action[/maroon][teal]=[/teal][green][i]"edit.asp"[/i][/green][b]>[/b][/small]
[b]<select[/b] [maroon]name[/maroon][teal]=[/teal][green][i]"EditStock"[/i][/green] [maroon]id[/maroon][teal]=[/teal][green][i]"EditStock"[/i][/green] [maroon]onchange[/maroon][teal]=[/teal][green][i]"FillText([highlight]this[/highlight]);"[/i][/green][b]>[/b]
[small][b]<option[/b] [maroon]value[/maroon][teal]=[/teal][green][i]"AA"[/i][/green][b]>[/b]1 One[b]</option>[/b]
[b]<option[/b] [maroon]value[/maroon][teal]=[/teal][green][i]"BB"[/i][/green][b]>[/b]2 Two[b]</option>[/b]
[b]<option[/b] [maroon]value[/maroon][teal]=[/teal][green][i]"CC"[/i][/green][b]>[/b]3 Three[b]</option>[/b]
[b]<option[/b] [maroon]value[/maroon][teal]=[/teal][green][i]"DD"[/i][/green][b]>[/b]4 Four[b]</option>[/b]
[b]</select>[/b]
[b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"text"[/i][/green] [maroon]id[/maroon][teal]=[/teal][green][i]"EditDescription"[/i][/green] [maroon]name[/maroon][teal]=[/teal][green][i]"EditDescription"[/i][/green] [b]/>[/b]
[b]</form>[/b][/small]

Feherke.
 
Thanks Feherke. That works perfect.

Out of interest I added

Code:
txtObj.value = selObj.options[selIndex].text.replace(/\w+ /,'');
[code]

Which i realise would remove letters as well as digits.

If I had the following:
<option value="AA">1[COLOR=red] - [/color]One</option>

Would it be easy to strip the [b]-[/b]?

Thanks.

Ah dinnae ken...
 
Hi

Just add it to the expression :
JavaScript:
txtObj[teal].[/teal]value [teal]=[/teal] selObj[teal].[/teal]options[teal][[/teal]selIndex[teal]].[/teal]text[teal].[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal][fuchsia]/\w+ - /[/fuchsia][teal],[/teal][green][i]''[/i][/green][teal]);[/teal]
Or if the dash ( - ) is not always present :
JavaScript:
txtObj[teal].[/teal]value [teal]=[/teal] selObj[teal].[/teal]options[teal][[/teal]selIndex[teal]].[/teal]text[teal].[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal][fuchsia]/\w+ -? */[/fuchsia][teal],[/teal][green][i]''[/i][/green][teal]);[/teal]

Feherke.
 
Hi

Or if the presence of dash may exclude the presence of space ( I mean, the separator can be dash or space or both ) :
JavaScript:
txtObj[teal].[/teal]value [teal]=[/teal] selObj[teal].[/teal]options[teal][[/teal]selIndex[teal]].[/teal]text[teal].[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal][fuchsia]/\w+[- ]+/[/fuchsia][teal],[/teal][green][i]''[/i][/green][teal]);[/teal]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top