digisnail
digisnail

Reputation: 13

jquery replaceWith problem

I'm trying to replace some table elements while clicking the modify link, actually it does replace, but the href attribute is not correct. Is there anything wrong? Please help, thanks!!

HTML:

<html>
<head>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
    <table id="department_list_table" border=1>           
        <tbody> 
            <tr id = 1>
                <td>department A</td>
                <td align="center"><a href = "#" onclick = "department_action('modify', 1, 'department A')">modify</a></td>
                <td align="center"><a href = "#" onclick = "department_action('delete', 1, 'department A')">delete</a></td>
            </tr>                
            <tr id = 2>
                <td>department B</td>
                <td align="center"><a href = "#" onclick = "department_action('modify', 2, 'department B')">modify</a></td>
                <td align="center"><a href = "#" onclick = "department_action('delete', 2, 'department B')">delete</a></td>
            </tr>                
        </tbody>
    </table>        
</body>

jQuery:

var id = 3;
var name = 'department C';
$("#department_list_table tbody tr#1").replaceWith(
    '<tr id =' + id + '>' +
    '<td>' + name + '</td>' +
    '<td align="center"><a href="#" onclick="department_action("modify",'+ id +',' + name + ')">modify</a></td>' +
    '<td align="center"><a href="#" onclick="department_action("delete",'+ id +',' + name + ')">delete</a></td>' +
    '</tr>');

Result in firebug:

<tr id="3">
<td>department C</td>
<td align="center">
**<a c)"="" modify",3,department="" onclick="department_action(" href="#">modify</a>**
</td>
<td align="center">
**<a c)"="" delete",3,department="" onclick="department_action(" href="#">delete</a>**
</td>
</tr>

Upvotes: 0

Views: 536

Answers (2)

Barum Rho
Barum Rho

Reputation: 2753

The double quotes in onclick attribute is messing up the html string. A solution is to use \'modify\', but you should consider attaching event handlers in JavaScript, using $().click. It generally makes the code more maintainable.

Upvotes: 0

SeanCannon
SeanCannon

Reputation: 77956

You're using double quotes when you should be using single quotes or escaped quotes:

onclick="department_action(\'modify\','+ i ...

Upvotes: 1

Related Questions