help to move jquery to javascript.

help to move jquery to javascript.

PaulVickeryPaulVickery Posts: 45Questions: 8Answers: 0

Please can someone help me as I am moving across to Datatables 3 from 2 and removing all JQuery.

I use the following but cannot "translate" it into javascript. If anyone can help me, I would be very greatful.

            editor.dependent("colour", function () {
                var label = editor
                    .field("colour")
                    .input()
                    .find("option:selected")
                    .text();
                editor.val("code", label);

                return {};
            });

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 65,999Questions: 1Answers: 10,990 Site admin
    Answer ✓
    .find("option:selected")
    

    This is the one bit that will trip you up since :selected is a jQuery extension and you'll probably be getting an invalid selector error with that?

    If you just need the value from the select list, use:

    var label = editor.field('colour').val();
    

    However, the label and the value are not always the same in a select (apologies if this is preaching to the choir!), so it could be that you want to actually get the text content for the selected option, in which case:

        var label = editor
            .field("colour")
            .input()
            .find("option")
            .filter(el => el.selected)
            .text();
    

    will do the job, exactly matching what you have with the jQuery selector.

    Allan

  • PaulVickeryPaulVickery Posts: 45Questions: 8Answers: 0

    Thank you very much for your swift answer. All working perfectly, thank you.

  • RichardD2RichardD2 Posts: 28Questions: 2Answers: 1
    edited September 18

    NB: The equivalent of jQuery's option:selected is option:checked. So:

    .find("option:selected")
    

    would become:

    .querySelector("option:checked")
    

    Or querySelectorAll for a multi-select list.

    Alternatively, you can use the selectedOptions collection.

  • allanallan Posts: 65,999Questions: 1Answers: 10,990 Site admin

    Doh - I should have remembered that. Many thanks!

    Allan

Sign In or Register to comment.