djb80
djb80

Reputation: 43

jquery ColdFusion return

I want to return the jquery results separately so I can build a table out of them

This already works

<script>            
            $(document).ready(function() {            
            
                    $result = $("#result");
                    
                    $("#options").change(function(e) {
                        var selected = $(this).val();
                        console.log('change:', selected);
                        if(selected === '--') return;
                        $.get("/inside/fusebox/view/communications/locations/getsite.cfc?method=dostuff", {input:selected}, function(res) {
                        
                            $result.html(res);
                        },"JSON");
                    });
            })
            </script>

It shows the return as one long string

However, I have them separated in the return.

<cfreturn "#siteinfo.photourl#,#siteinfo.siteid#,#siteinfo.sitename#,#siteinfo.address#,#siteinfo.city#,#siteinfo.state#,#siteinfo.zipcode#" />

so what appears on the page of one long sentence

in this div

<div id="result"></div> 

It works.

I just really don't know how to go about and separate the results so I can build a table as a display.

<table> <tr>
        <td><div id = "siteid"></div></td>
        <td><div id = "sitename"></div></td>
        </tr>
<tr><td><div id = "siteaddress"></div></td>
        <td><div id = "sitecity"></div></td>
        <td><div id = "siteastate"></div></td>
        <td><div id = "sitezipcode"></div></td></tr>
</table>

Upvotes: 2

Views: 60

Answers (1)

PeterKA
PeterKA

Reputation: 24638

Welcome to SO! Hope you like it here.

You're better of returning JSON such as:

[{"photourl":"...", "siteid": "...", "sitename": "<sitename>", "city": "XX", "state": "ZZ", "zipcode": "00000"}];

Then you would replace

$result.html(res);

With

const siteinfo = JSON.parse(res)[0];
with(siteinfo) {
    $('#siteid').text(siteid);
    $('#sitename').text(sitename);
    //etc
}

If you intend to return multiple structs (objects) which will result in multiple rows consider using classes, eg .siteid, .sitename ...., instead of ids.

Upvotes: 2

Related Questions