0

I'm trying to take some data from my javascript file and put it into new rows and columns in my html file. I think the problem might have to do with my script closing before the table in my html file. I currently have a table in my html file that looks like this:

<div id="map" style="width: 700px; height: 700px"></div>
            <script src="js/mapping.js"></script>


            <div id="tablebody">
            <p>
                <b>List of Nearby Locations</b>
            </p>
                <table class="table table-stripped">
                    <tbody>
                        <tr>
                            <th>Name</th>
                            <th>Address</th>
                        </tr>
                        <tr>
                            <td>John Smith</td>
                            <td>1500 Pennsylvania Ave</td>
                        </tr>
                    </tbody>
                </table>
            </div>

and JS code that looks like this:

service = new google.maps.places.PlacesService(map);
    service.nearbySearch(request, callback);


    function callback(results, status) {
        if (status == google.maps.places.PlacesServiceStatus.OK) {

            for (var i = 0; i < results.length; i++) {
                createMarker(results[i]);
                alert("name: " + results[i].name + ", Address:" + results[i].vicinity);
                $('#tablebody tr:last').after('<tr>"Name: " + results[i].name + ", Address: " + results[i].vicinity</tr>');
            }
        }
    }

2 Answers 2

1

You are missing table cells in the table row, and I assume that you want to concatenate the name and vicinity into the code, not just showing Javascript code in the table:

$('#tablebody tr:last').after('<tr><td>Name: ' + results[i].name + ', Address: ' + results[i].vicinity + '</td></tr>');

Or perhaps with two cells in the row:

$('#tablebody tr:last').after('<tr><td>' + results[i].name + '</td><td>' + results[i].vicinity + '</td></tr>');
Sign up to request clarification or add additional context in comments.

Comments

1

Try this for javascript based :

  tableObject.insertRow(index)

Or jQuery

 $('#tablebody').append('<tr>"Name: " + results[i].name + ", Address: " + results[i].vicinity</tr>');

1 Comment

make a fiddle real quick, and will get it working for you. Not with the API stuff, just the table code, and drop the google info. It is a jquery selector/tag issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.