简体   繁体   中英

How to append an unordered list to en exisiting html element using jquery

<input class ="metro" type="text" name="city">

This is input tag as a child of which I need to append an unordered list which is passed as an array to a function call as follows

$("input").autoComplete(["agra","bombay","delhi","chennai"]);

This autocomplete function accepts an array and outputs its elements as an unorederd list which I am not able to do.

$("input").autoComplete({source:["agra","bombay","delhi","chennai"]});

Ref

Demo

$(function () {
    var availableCities = [
        "agra",
        "bombay",
        "delhi",
        "chennai"
    ];

    $("input.metro").autocomplete({
        source: availableCities
    });
});

or

$(function () {
    $("input.metro").autocomplete({
        source: [
            "agra",
            "bombay",
            "delhi",
            "chennai"]
    });
});

Demo

API DOCS

If you want to implement the function on your own, the basic block would be as follows

$(function () {
    var Cities = [
        "agra",
        "bombay",
        "delhi",
        "chennai"
    ];

    $("input.metro").autocomplete({
        $(this).append("<ul>");
        for(i=0;i<Cities.length;i++){
            $(this).children("ul").append("<li>Cities[i]</li>");
        }
        $(this).append("</ul>");

    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM