views:

37

answers:

2

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.

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
+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
hey nick congrats on 100k
mcgrailm
+1 better than mine, no wonder you're #1 on the weekly rankings :)
Fosco