Jenny
Jenny

Reputation: 1735

how to write javascript function as string in php?

I have one line in php file like this:

<a class="hide-if-no-js" onclick="findPosts.open( 'media[]','<?php echo $post->ID ?>' ); return false;" href="#the-list">Attach</a>

I need to write this line as a string in a php function, I tried this:

$output .="<a class='hide-if-no-js' onclick='findPosts.open( 'media[]','$id' ); return false;' href='#the-list'>'$linktext'</a>";

Seems the quote around 'media[]' is wrong and made the html of this line messed up. Could anybody help correct me?

Upvotes: 0

Views: 598

Answers (2)

xdazz
xdazz

Reputation: 160943

$output .= "<a class=\"hide-if-no-js\" onclick=\"findPosts.open( 'media[]','$id' ); return false;\" href=\"#the-list\">$linktext</a>";

Your javascript string use single quote, so the html attribute should use double quote.

Upvotes: 2

Marcio Mazzucato
Marcio Mazzucato

Reputation: 9305

You can try:

$output .= '<a class="hide-if-no-js" onclick="findPosts.open( \'media[]\',\'' . $id . '\' ); return false;" href="#the-list">' . $linktext . '</a>';

OR

$output .= "<a class='hide-if-no-js' onclick='findPosts.open( \"media[]\", \"$id\" ); return false;' href='#the-list'>$linktext</a>";

Upvotes: 1

Related Questions