1

I have the following Javascript function that allows me to add a new row to my table:

$(function() {

var $table = $('table.pv-data'),
    counter = 1;

$('a.add-author').click(function(event){
    event.preventDefault();
    counter++;            
var newRow = 
        '<tr>' + 
            '<td><input type="text" name="id' + counter + '"/></td>' +
            '<td><select name="state' + counter + '"/><OPTION value= "persil">Persil</OPTION><OPTION value= "other">Other</OPTION></select></td> ' +
            '<td><input type="text" name="longitude' + counter + '"/></td>' +
            '<td><input type="text" name="latitude' + counter + '"/></td>' +
            '<td><input type="text" name="altitude' + counter + '"/></td>' +
            '<td><input type="text" name="module_tilt' + counter + '"/></td>' +
            '<td><a href="#" class="remove">remove</a></td>'
        '</tr>';
    $table.append(newRow);
    });
});

Everything is working fine, just the part where I have to select between the options:"Persil" or "Other" is not working. it's not showing the different options.

5
  • 1
    Of course won't work. You are closing the select tag too early. <select name="state'+counter'"> should be correct Commented Jun 26, 2013 at 15:14
  • @KiaMorot Oh ok, didn't pay attention to that Commented Jun 26, 2013 at 15:16
  • As a side note, if you do a lot of this, you may want to consider using mustache.js Commented Jun 26, 2013 at 15:17
  • 1
    FYI, you're missing a + after the second to last line of HTML. It's actually harmless here, but it excludes the closing </tr>. Commented Jun 26, 2013 at 15:18
  • @KiaMorot Write this as an answer and I'll +1 Commented Jun 26, 2013 at 15:18

3 Answers 3

2

The following lines need to be replaced

  1. '<td><select name="state' + counter + '"/>...'. Is not correct as you are closing the select tag too early.
  2. '<td><a href="#" class="remove">remove</a></td>'. You are missing + at the end.

by these replacements:

  1. '<td><select name="state' + counter + '">...'.
  2. '<td><a href="#" class="remove">remove</a></td>' +

Note: Replacement#2 is pointed out in the comments.

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

Comments

2

Take away the self-closing tag from the end of the opening select tag and remove the space between the value= "persil" in both of the option elements. Also add a + to concatenate the final </tr>.

var newRow = 
    '<tr>' + 
        '<td><input type="text" name="id' + counter + '"/></td>' +
        '<td><select name="state' + counter + '"><OPTION value="persil">Persil</OPTION><OPTION value="other">Other</OPTION></select></td> ' +
        '<td><input type="text" name="longitude' + counter + '"/></td>' +
        '<td><input type="text" name="latitude' + counter + '"/></td>' +
        '<td><input type="text" name="altitude' + counter + '"/></td>' +
        '<td><input type="text" name="module_tilt' + counter + '"/></td>' +
        '<td><a href="#" class="remove">remove</a></td>' +
    '</tr>';

Having the self closing tag at the end of an element (the slash at the end, as in <img src="..." />) is only for elements without an independent closing tag, so for something like <select> you do not need it as this function is fulfilled by the </select>.

Comments

0

You have a problem in this line:

'<td><select name="state' + counter + '"/><OPTION value= "persil">P

The select have it own closing tag it must be like this

'<td><select name="state' + counter + '"><OPTION value= "persil">P

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.