javascript - Function parameter used in jQuery selector -


i have event handler appends new row table.

$("#pre_script_12").change(function() {     $("#selected_pre_scripts")         .append("<tr><td><a href='javascript:movescriptup("             + this.id             + ")'>move</a></td></tr>"); 

movescriptup() looks this:

function movescriptdown(id) {     current = $("tr." + id).eq(1);     $(current).prev().before($(current).next());     $(current).prev().before($(current).next()); } 

however, getting error when when appending id tr..

error: syntax error, unrecognized expression: tr.[object htmlinputelement] 

how can supply id parameter above javascript function without getting error when attempting use in jquery selector?

you need quote strings in function call.

right now, <a> tag looks this:

<a href='javascript:movescriptup(pre_script_12)'>move</a> 

in (most) browsers, elements ids become global variable, pre_script_12 refers element. need add quotes, you're passing string:

$("#selected_pre_scripts")     .append("<tr><td><a href='javascript:movescriptup(\""         + this.id         + "\")'>move</a></td></tr>"); 

p.s. highly reccomend, not using inline javascript. why not bind (delegated) event handler call movescriptup() function?


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -