Win a copy of Securing DevOps this week in the Security forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Retrieving the text of the selected option in a select element  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following dropdown field



I need to get the option text and not the value with java script...

This is not working:


Any help is appreciated...
Thanks
 
Marshal
Posts: 4343
270
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're going to need to expand a little on "This is not working".

However, it does work for me. It also works using .innerHTML in place of .text
 
peter meyer
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim Cooke

I have this Java Script section in my php file...
And it will not take my text of the selected option.

How did you made you test?
If I would like to test it in a blank html file only with with my dropdown field and the java script?
Where and how do I have to set my "console.log()" to see the resulat?


Thanks a lot...
 
Tim Cooke
Marshal
Posts: 4343
270
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tested with a very simple HTML page. I'm not a very good front end developer and I'm sure more seasoned JavaScript engineers, i.e. Bear, will scald me for using an alert() rather than console logging but it's the easiest thing I can do off the top of my head.
 
Author and ninkuma
Marshal
Posts: 66490
160
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Life is too short not to be using jQuery for stuff like this, where the .val() method on the select element gets you the selected value.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66490
160
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Cooke wrote:Bear, will scald me for using an alert() rather than console logging but it's the easiest thing I can do off the top of my head.



 
We noticed he had no friends. So we gave him this tiny ad:
Thread Boost - a very different sort of advertising
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!