0

Let's say we have a domain class Country with the next relationship:

Town town

And a domain class Town with:

Integer id
String town

The select works well fine with the following line of code (I'm using Grails Fields plugin):

<f:field bean="countryInstance" property="town" input-optionValue="town"/>

I want to get the optionValue, but the next code examples get the id:

var townName= document.getElementById("town").value;
var townName= document.getElementById("town").options[document.getElementById("town").selectedIndex].value;

The next code didn't work (this is the attempt to navigate through classes with Javascript):

var townName= document.getElementById("town").town.value;

How could I get the optionValue (or navigate through the relationship) to get String town instead Integer id?

chelder
  • 3,819
  • 6
  • 56
  • 90

1 Answers1

2

Using plain javascript, jsfiddle, question in SO

var el = document.getElementById("town");
// value
el.options[el.selectedIndex].value;
// text
el.options[el.selectedIndex].text;

Or with jQuery, question in SO:

$("#town option[value='2']").text()
$("#town option:selected").text()
Community
  • 1
  • 1
Ivar
  • 4,350
  • 2
  • 27
  • 29