Obtain the Select Label as well as Value

Obtain the Select Label as well as Value

blabablabablabablaba Posts: 47Questions: 19Answers: 0

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

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Hi Steve,

    If you need to get the label from a select field use:

    editor.field('myField').input().children(':selected').text()
    

    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 the dependent() method is a good way of doing it.

    Allan

This discussion has been closed.