How do I feed string variable holding multiline value in it to jquery method like prepend? I need it to construct a form pass by drupal search module like - $('#divsearch').prepend('<?php print $search_box; ?>').
Thanks a bunch.
views:
37answers:
2
A:
I think the problem is that you can have carriage returns or new lines in you string lets take out the php and show what i mean
$('#divsearch').prepend('<div />');
this would work
$('#divsearch').prepend('<div>some text that goes
in a div</div>');
this does not work
$('#divsearch').prepend('<div>some text that goes'+
'in a div</div>');
this woould
mcgrailm
2010-10-27 13:00:45
+2
A:
Use json_encode()
to encode the string, like this:
$('#divsearch').prepend('<?php print json_encode($search_box); ?>');
This will encode newlines as \n
instead of literally rendering them...giving you your current syntax errors.
Nick Craver
2010-10-27 13:01:09
hey nick congrats on 100k
mcgrailm
2010-10-27 13:04:15
+1 better than mine, no wonder you're #1 on the weekly rankings :)
Fosco
2010-10-27 13:05:11