eatmailyo
eatmailyo

Reputation: 670

location.href in echo doesnt work

I have some little problem with my php echo. I have a JavaScript link function and that doesnt work ,because in html it shows this type of link onclick="location.href="http://www.link.com/";" As you can see ,this dont work with this syntax. But with this onclick="location.href='http://www.link.com/';" that works well

My php look like this

...
echo '<div ... onclick="location.href="'.get_permalink($recent["ID"]).'";">..</div>';
...

What I need to do ,to this works properly?

Thank you!

Upvotes: 1

Views: 715

Answers (1)

Mr. Hugo
Mr. Hugo

Reputation: 12592

You used the wrong quotes and/or did not escape them.

Here is the solution:

echo '<div ... onclick="location.href=\''.get_permalink($recent["ID"]).'\';">..</div>';

Cleaner/better is to NOT use HTML in your PHP, like this:

<div ... onclick="location.href='<?php echo get_permalink($recent["ID"]); ?>';">..</div>

Upvotes: 8

Related Questions