Obtain the Select Label as well as Value
Obtain the Select Label as well as Value
Hi Allen,
Within Editor, I want to use the information in the select field LABEL rather than it's VALUE/ID.
This is how I achieved it.
I have a select which obtains options data via ajax in the form:
{"Edinburgh":0,"London":1}
This brings up a nice select/select2 list with Edinburgh and London.
Within Editor, I set up a dependent field to 'do something' with the select field LABEL rather than it's VALUE/ID.
To achieve this, I used an external function getKeyByValue
function getKeyByValue(object, value) {
return Object.keys(object).find(key => object[key] === value);
}
And within editor depentent field:
editor.dependent( 'table_ID', function ( val ) {
if(val !== null) {
alert('Value selected is '+getKeyByValue(table.ajax.json()['options']['table_ID'], Number(val));
}
}
In Editor, is this the best / most efficient way to get the Label value? Or does Editor offer the label value in a more readily available field?
Kind regards,
Steve
This question has an accepted answers - jump to answer
Answers
Hi Steve,
If you need to get the label from a
select
field use:That will get the selected
option
element and then grab its text value. If you want to get that whenever you are changing the value, then yes thedependent()
method is a good way of doing it.Allan