How to get value of Select2 with crudfields js? #386
-
i have a select2_from_ajax field that gets its data from a fetch operation, and a 'custom html' element with an img tag. I want to be able to change the image displayed depending on the item selected in the 'select2_from _ajax'. The switch method for selects from the docs page doesnt seem to be working on select_2. How can I access the selected value in the select_2? My code:
}).change(); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
i figured this out, leaving this here if anyone ever needs it. to use with select_2, you have to put quotes around the values you are checking for. crud.field('landing_page_id').onChange(function(field) {
switch(field.value) {
case '1': // <---- here, it should be checked against string, not integer.
source = "/images/landing_page/brand.png";
break;
case '2': // <---- here, it should be checked against string, not integer.
source = "/images/landing_page/c_1.png";
break;
default:
source = "/images/landing_page/empty.png";
}
$('img[id="lander-thumb"]').attr('src', source);
}).change(); |
Beta Was this translation helpful? Give feedback.
i figured this out, leaving this here if anyone ever needs it.
to use with select_2, you have to put quotes around the values you are checking for.
This works for me: