[SOLVED] Extract text from a dropdown field? Hmm...

This is a discussion on "[SOLVED] Extract text from a dropdown field? Hmm..." within the JavaScript Forum section. This forum, and the thread "[SOLVED] Extract text from a dropdown field? Hmm... are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Nov 3rd, 2007, 22:01
Junior Member
Join Date: Jun 2006
Location: UK
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Question [SOLVED] Extract text from a dropdown field? Hmm...

Hi everyone,

I'm not sure if what I want to do is possible, but I hope it is! Would anybody be able to help me?

I have a dropdown menu with a number of options.

For the text that is displayed for each option, it looks something like:

[123456] Some text - Some more text

What I would like to do, is extract just the text between '[' and ']'. I will then display this on the page. I know how to do this, but just need the correct text to display.

I hope someone could help, becuase I've searched all over the internet and have found nothing!

Many thanks
Reply With Quote

  #2 (permalink)  
Old Nov 3rd, 2007, 23:29
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Extract text from a dropdown field? Hmm...

If it will always be numbers you could use a simple regex...

alert(whereveryougetthetextfrom.match(/\[[0-9]*\]/));
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #3 (permalink)  
Old Nov 4th, 2007, 17:18
Junior Member
Join Date: Jun 2006
Location: UK
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Extract text from a dropdown field? Hmm...

It won't always be numbers but thanks anyway for your help.

Is 'alert' to display a box that pops up? I thought it was (I don't know much about javascript though).

When I said I wanted to display it somewhere else on the page, I meant that I would place a <span> and then change the contents of that using some code I found, involving 'innerhtml'.

Many thanks
Reply With Quote
  #4 (permalink)  
Old Nov 4th, 2007, 18:24
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Extract text from a dropdown field? Hmm...

Okay, so I was only using an alert to show how you would get the details...

If it will be in square brackets and only letters and numbers.

you could use.

Code: Select all
var theText = whereveryougetthetextfrom.match(/\[([0-9A-Z]*)\][\s]{1}/i)[0];

theSpan.innerHTML = theText;
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #5 (permalink)  
Old Nov 4th, 2007, 22:40
Junior Member
Join Date: Jun 2006
Location: UK
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Extract text from a dropdown field? Hmm...

Aaaah okay, sorry.

Right I tried that but it didn't work, so I had a little fiddle around with the code and got it to work.

What I ended up with was:
Code: Select all
var theText = document.getElementById('dropdown').options[document.getElementById('dropdown').selectedIndex].innerHTML.match(/\[([-0-9A-Z]*)\][\s]{1}/i)[0];
document.getElementById('span').innerHTML = theText;
Thanks for your help, I wouldn't have been able to do it without it.

Simon
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
making text field text disapear Phixon JavaScript Forum 4 Feb 2nd, 2008 07:49
[SOLVED] Text field &amp; list menu size problem longstand Web Page Design 2 Nov 1st, 2007 12:38
Non-text field Validation NewDesigner JavaScript Forum 6 Nov 24th, 2006 22:34
validate text field to db field jtesolin Classic ASP 1 Jul 18th, 2006 17:48
Building Text/Dropdown Menu w/ no background TruBluTex Flash & Multimedia Forum 8 Jan 15th, 2006 20:54


All times are GMT. The time now is 10:28.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43