How to get the innerHTML of selectable jquery element?

后端 未结 4 1483
礼貌的吻别
礼貌的吻别 2021-01-01 08:49

I have a php generated list whose list items are selectable using jquery selectable widget. The list for all intents and purposes is:

相关标签:
4条回答
  • 2021-01-01 09:18

    The parameter ui has a property called selected which is a reference to the selected dom element, you can call innerHTML on that element.

    Your code $('.ui-selected').innerHTML tries to return the innerHTML property of a jQuery wrapper element for a dom element with class ui-selected

    $(function () {
        $("#select-image").selectable({
            selected: function (event, ui) {
                var $variable = ui.selected.innerHTML; // or $(ui.selected).html()
                console.log($variable);
            }
        });
    });
    

    Demo: Fiddle

    0 讨论(0)
  • 2021-01-01 09:21

    Use .val() instead of .innerHTML for getting value of selected option

    Use .text() for getting text of selected option

    Thanks for correcting :)

    0 讨论(0)
  • 2021-01-01 09:24

    Try

    .text() or .html() instead of .innerHTML

    0 讨论(0)
  • 2021-01-01 09:31
    $(function() {
            $("#select-image").selectable({
                selected: function( event, ui ) { 
                    var $variable = $('.ui-selected').html(); 
                    console.log($variable);
                }
            });
        });
    

    or

    $(function() {
            $("#select-image").selectable({
                selected: function( event, ui ) { 
                    var $variable = $('.ui-selected').text(); 
                    console.log($variable);
                }
            });
        });
    

    or

    $(function() {
            $("#select-image").selectable({
                selected: function( event, ui ) { 
                    var $variable = $('.ui-selected').val(); 
                    console.log($variable);
                }
            });
        });
    
    0 讨论(0)
提交回复
热议问题