简体   繁体   中英

using integer received on js function as param to fetch div id with same id as passed integer as param

I'm receiving some integer on js function as parameter and I want to use that value which also represents div id to append result inside div.

function DisplaySomeData(someData, code) {
        $.ajax({
            url: formatUrl('...'),
            type: 'POST',
            contentType: 'application/json',
            data: JSON.stringify({ data: someData}),
            success: function (result) {
                $("#+code").html(result);// how to use code to recognize div id?
            },
            error: ...
        });

You aren't concatenating the selector string correctly. Try this:

$('#' + code).html(result)

Some Syntax error in your code. If you want to select div whose id attribute 'code' which you are passing in DisplaySomeData function. below is modified syntax.

function DisplaySomeData(someData, code) {
    $.ajax({
        url: formatUrl('...'),
        type: 'POST',
        contentType: 'application/json',
        data: JSON.stringify({ data: someData}),
        success: function (result) {
            $("#"+code).html(result);// or use $("div[id='"+code+"']").html(result);
        },//Both the syntax should work
        error: ...
    });

Note : If you found this useful. Please mark it as answered.

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