开发者

append function apostrophe

开发者 https://www.devze.com 2023-02-10 01:36 出处:网络
I am appending the following: $(\'#field\').append(\"<input type=\'text\' id=\'searchfield\'placeholder=\'Search...\' autosave=\'save\' results=\'5\' onkeyup=\'onChange(\'searchfield\',\'searchfie

I am appending the following:

$('#field').append("<input type='text' id='searchfield'  placeholder='Search...' autosave='save' results='5' onkeyup='onChange('searchfield','searchfield_right')' />")

it's appending, but the onChange function is not working due to the apostrophe...

onkeyup="onChange('searchfield','searchfield_right')"

it should look like above with " and ' for 'searchfield','searhfield_right'

when appending you have to use ' only so the 'searchfield','searhfield_right' would be seen as "searchfield","searchfield_right" which is not correct -

is there a different way to append the right apostrophe开发者_Go百科 ' for the function????


Your attribute value is delimited with ' characters. So if you try to use a literal ' inside the value, it will terminate the value instead of being part of it.

onkeyup='onChange('

Represent it with an entity (&#39;). Better yet (much better in fact), use unobtrusive JavaScript: attach event handlers with JavaScript, not HTML attributes.


Just escape them.

$('#field').append("<input type='text' id='searchfield'  onkeyup='onChange(\"searchfield\",\"searchfield_right\");' />");

However, I agree with David that you should use event handlers.


Instead of passing string variables, just pass this and the event:

onkeyup="onChange(this, event)"

Then in your onChange function, you can use:

function onChange(element, event) {
   var id = element.id,

}

If however you are absolutely certain you want to pass strings, escape the quotations:

onkeyup="onChange(\"searchfield\",\"searchfield_right\")"

---> Demo <---

0

精彩评论

暂无评论...
验证码 换一张
取 消