0

I want to append some values from jQuery in my HTML table

$(document).ready(function() {
  console.log("ready!");

  // on form submission ...
  $('form').on('submit', function() {

    console.log("the form has beeen submitted");

    // grab values
    valueOne = $('input[name="perfid"]').val();
   
    console.log(valueOne)

    $.ajax({
      type: "POST",
      url: "/",
	  datatype:'json',
      data : { 'first': valueOne},
      success: function(result) {
		console.log(result.result[0].userid);
       
		
		$('#result').html("<table class="table table-bordered responsive"><tr><td>" + result.result[0].hosts[0].filer + "</td><td>" + result.result[0].hosts[0].hostname + "</td><td>" + result.result[0].hosts[0].model + "</td></tr></table>");
		
       
      },
      error: function(error) {
        console.log(error)
      }
    });

  });

});

I am getting error as : SyntaxError: missing ) after argument list for the line :

$('#result').html("<table class="table table-bordered responsive"><tr><td>" + result.result[0].hosts[0].filer + "</td><td>" + result.result[0].hosts[0].hostname + "</td><td>" + result.result[0].hosts[0].model + "</td></tr></table>");

Can someone point out what is going wrong? TIA

1
  • 4
    Use quotes appropriately when nesting inside each other. $('#result').html('<table class="table table-bordered responsive"><tr><td>" + result.result[0].hosts[0].filer + "</td><td>" + result.result[0].hosts[0].hostname + "</td><td>" + result.result[0].hosts[0].model + "</td></tr></table>'); Commented Dec 16, 2015 at 6:16

2 Answers 2

1

its all about quotes

$('#result').html('<table class="table table-bordered responsive"><tr><td>' + result.result[0].hosts[0].filer + '</td><td>' + result.result[0].hosts[0].hostname + '</td><td>' + result.result[0].hosts[0].model + '</td></tr></table>');

take a look at How to concatenate variable in string in javascript

Sign up to request clarification or add additional context in comments.

3 Comments

I have tried this but I cannot get anything displayed with it. The div tag with result class stays blank.
@Akki by using $('#result') that mean you have element with result id not class if its class you should use $('.result')
@Akki glad it helped .. Good Luck :)
0

Please try below code

var table = $('<table/>').addClass('table table-bordered responsive');
var rows='<tr><td>' + result.result[0].hosts[0].filer + '</td><td>' + result.result[0].hosts[0].hostname + '</td><td>' + result.result[0].hosts[0].model + '</td></tr>'; 
$(table).append(rows);
$('#result').append(table);

Comments

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.