3

Using the code below I can insert a new row to my table (#courseelements).

$("#addbutton").click(function() {
      $('#courseelements > tbody:last').append('<tr><td>Helloworld</td></tr>');
    });

However, I'd like to replace the Helloworld with a <select> element. So something like this:

$("#addbutton").click(function() {
      $('#courseelements > tbody:last').append('<tr><td>

        <select class='plan_id'>
            <option value='1'>Description</option>
            <option value='2'>Description</option>
            <option value='3'>Description</option>  
        </select>


      </td></tr>');
    });

But this gives me "missing ) after argument list" on the first apostrophe ' <select class='

Any ideas why?

David
  • 1,171
  • 8
  • 26
  • 48
  • des got it before I could, but you can even see in your question, the coloring is different for plan_id because it is being parsed as outside of the string – Alex W Jun 20 '12 at 16:41
  • 1
    (This is why it pays to use an *appropriate* editor -- the kind that does syntax highlighting, indenting, perhaps even static analysis, etc...) –  Jun 20 '12 at 16:42

6 Answers6

4

You forgot to escape quotes:

$("#addbutton").click(function() {
  $('#courseelements > tbody:last').append('<tr><td>

    <select class=\'plan_id\'>
        <option value=\'1\'>Description</option>
        <option value=\'2\'>Description</option>
        <option value=\'3\'>Description</option>  
    </select>


  </td></tr>');
});

As alternative you can wrap whole appended string in double quotes insetead of single quotes:

$("#addbutton").click(function() {
  $('#courseelements > tbody:last').append("<tr><td>

    <select class='plan_id'>
        <option value='1'>Description</option>
        <option value='2'>Description</option>
        <option value='3'>Description</option>  
    </select>


  </td></tr>");
});
Zbigniew
  • 27,184
  • 6
  • 59
  • 66
1
var select=$("<select class='plan_id'></select>")
    .append("<option value='1'>Description</option>")
    .append("<option value='2'>Description</option>")
    .append("<option value='3'>Description</option>");

 $('#courseelements > tbody')
.append($("<tr><tr>"))
.append($("<td></td>")
.append(select));

DEMO.

The Alpha
  • 143,660
  • 29
  • 287
  • 307
1

You need to use a different quote type for the quotes inside your HTML than you do for the quotes surrounding the HTML:

$("#addbutton").click(function() {
      $('#courseelements > tbody:last').append("<tr><td>

        <select class='plan_id'>
            <option value='1'>Description</option>
            <option value='2'>Description</option>
            <option value='3'>Description</option>  
        </select>


      </td></tr>");
    });
Jacob Mattison
  • 50,258
  • 9
  • 107
  • 126
1

You have to use different quote marks.

select class="plan_id">

double here since the entire string is delimited with single.

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
1

change the single quotes with double quotes inside the string-

<select class="plan_id">
            <option value="1">Description</option>
            <option value="2">Description</option>
            <option value="3">Description</option>  
        </select>

or escape them with \ like-

<select class=\'plan_id\'>......
Avisek Chakraborty
  • 8,229
  • 10
  • 48
  • 76
0

And if you want to call $('').append through PHP echo, like this:

$info= "<div style=\'font-size:9px;\'>
    <img src=\'./img/filtro.png\'>Some tips
</div>";
echo "<script type='text/javascript'>
    $('#divTips').append('$info'); 
</script>";

Make sure replacing all your break lines before, like this:

$info= preg_replace( '/\s+/', ' ', $info);