How to get selectedIndex value for select type filed?

This is what works in Javascript:

var x = document.getElementById(“mySelect”).selectedIndex;

Is there a way to read and set Index of select filed in Frappe?

I need to synchronize two select fields = if first one is index 2 also second is set to index 2

Values in both lists are diferent so frm.set_value does not work for this.

Not sure if you could work by index, but if one field is the “master” as in one field is always set first, you could set the other field with if statements and frm.set_value. Wouldn’t even need to be a select field.

As I wrote values are different in lists. For example first list 1,2,3,4 and second A, B, C, D

It wouldn’t matter if they are different. You could write a custom script that checks a field for value “1”. If value “1” is present then add value “A” to another field. Both lists values would be hard coded in the script and the first field is a select field so users will only enter initial values the script recognizes.

Example custom script:

Doctype name: Doctype
custom1: select field with values 1,2,3
custom2: data field (should probably be read-only)

frappe.ui.form.on("Doctype", {
"custom1": function(frm) {
        if (frm.doc.custom1=='1') {
          (frm.set_value('custom2', 'A'));
        }
        else if (frm.doc.custom1=='2') {
          (frm.set_value('custom2', 'B'));
        }
        else if (frm.doc.custom1=='3') {
          (frm.set_value('custom2', 'C'));
        }
        else if (frm.doc.custom1=='') {
          (frm.set_value('custom2', ''));
        }
     }
});

As I said earlier this would work if users are only setting the value on one custom field and the other is always auto set by the script. Which is why the second custom field should be read-only. The second custom field does not need to be a select field since the values are hard coded into the script.

1 Like

Instead of If else you can user mapper.

var mapper = {“1”:“A”, “2”:“B”}
frm.set_value(‘custom2’, mapper[frm.doc.custom1])

3 Likes

how to select values of drop down?

@Dbone this is not work with me , i did like this script exactly
but i can’t change value of field depend on my selection value of another field, check my script and help me please if you can
@suyash