How to get the innerHTML of selectable jquery element?
Asked Answered
I

4

58

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

<ul id="#select-image">
    <li class="ui-widget-content">Item 1</li>
    <li class="ui-widget-content">Item 2</li>
    <li class="ui-widget-content">Item 3</li>
    <li class="ui-widget-content">Item 4</li>
    <li class="ui-widget-content">Item 5</li>
    <li class="ui-widget-content">Item 6</li>
    <li class="ui-widget-content">Item 7</li>
</ul>

And the jQuery selectable is declared as:

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

An event takes place after a list item has been selected, in the example it outputs to the browser console. The output however is "undefined." The selector $('.ui-selected'). is correct as it shows as an object in the browser's console. Where am I going wrong?

Inca answered 13/9, 2013 at 12:13 Comment(3)
I think innerHTML() only works for document elements, not jQuery objects. Use .html() insteadFiredog
<ul id="#select-image"> would not be matched by $("#select-image"), you need to make it <ul id="select-image"> for the selector to match it. I'm not sure if this is the case in your actual HTML or just a typo in the sample above.Avert
– Kiro Coneski is has found the problem and the solution. I suggest he posts it as an answer so I can give it a green tick.Inca
N
95

Try

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

Nadabus answered 13/9, 2013 at 12:16 Comment(1)
html() worked for me.Obstruct
L
11

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

Use .text() for getting text of selected option

Thanks for correcting :)

Lani answered 13/9, 2013 at 12:14 Comment(0)
H
5
$(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);
            }
        });
    });
Hiers answered 13/9, 2013 at 12:26 Comment(0)
U
0

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

Unhandsome answered 13/9, 2013 at 12:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.