Javascript equivalent of PHP's "print" [duplicate] - javascript

This question already has answers here:
Is there a php echo/print equivalent in javascript
(9 answers)
Closed 8 years ago.
If I have some PHP HTML that reads like this:
<html> I am feeling <?php print urldecode($_GET["emotion"]);?> today!</html>
and Get's "emotion" in URL title is "Happy", the HTML renders "I am feeling Happy today!".
Now, migrating away from PHP, how do I do this with javascript?
In Javascript I have a variable $emotion = "Happy"; so what Javascript goes inbetween the script tags below (where there are currently asterisks********)
<html> I am feeling <script>*********************</script> today!</html>

You can do it easily by putting an element where you want the text to change, then accessing that element by it's id.
$emotion = "Happy";
var e = document.getElementById('emotion');
e.innerHTML = $emotion;
I am feeling <span id="emotion"></span> today!

Related

How to include a PHP variable in Javascript code? [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 5 years ago.
[1] http://imgur.com/a/Ny8f7 "html code"
[2] http://imgur.com/a/kzEN2 "php code"
I try to use the variables from php in javascript to generate a chart with them.
Try using an id e.g.
In your html
name='someName'>
Then use that ID in your JS.
Use can use something like this:
var data = '<?=$dataFromPhp?>';

How do I translate HTML code into normal text with javascript? [duplicate]

This question already has answers here:
HTML Entity Decode [duplicate]
(17 answers)
Closed 6 years ago.
I want to translate an input from an API like this
<p>Communication that doesn’t take a chance doesn’t stand a chance.</p>
into something like this
"Communication that doesn’t take a chance doesn’t stand a chance."
I understand that html could translate this but I don't know how to translate this directly for use in javascript
Insert it into an element as HTML, and read it back out as text.
var elt = document.createElement('div');
elt.innerHTML = apiResponse;
alert(elt.textContent);

PHP $_Get and JQuery .load [duplicate]

This question already has answers here:
Encode URL in JavaScript
(22 answers)
Closed 8 years ago.
I want to load another page via Javascript like this
$("#resultarea").load("searchresults.php?searchstring=" + $("#searchbox").val());
in searchresults.php, a single statement
echo $_GET["searchstring"];
what I type in searchbox appears when searchresults.php is loaded except when I add space and another word, no thing appear at all. I heared it can be done by encoding or somewhat, I searched but didn't find a solution.
Try encodeURIComponent:
var encodedValue = encodeURIComponent($("#searchbox").val());
$("#resultarea").load("searchresults.php?searchstring=" + encodedValue);
Source
Demo

Injecting PHP data into JavaScript code [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
What's the preferred way of outputting PHP code within apostrophes? For example this JavaScript code would break and the second line will be totally empty:
var jsString = '<div id="content">'+'<?php echo $something['1'] ?>'+'</div>';
Thanks! I know this is kind of basic and I know how to fix it, but I'm wondering how to do it the right way?
[edit]
I know it looks obvious, but $something["1"] won't work. The same goes for $something[\'1\'].
This example is working well for me :
<?php
$something['1']='hhhh0';
?>
<script type='text/javascript'>
var jsString = '<div id="content">'+'<?php echo $something['1'] ;?>'+'</div>';
alert(jsString);
</script>

How to strip out all html tags from string [duplicate]

This question already has answers here:
Strip HTML from Text JavaScript
(44 answers)
Closed 8 years ago.
Here is the code I have:
I want to make sure the user doesn't put any html tags in the "Add Responsibilities" field. So for example, if the user writes this:
<div>Test</div>
Then it would put this into the responsibility field:
Test
I think it's something to do with this command but I can't get it working within my code:
$("#resp_input").html( $("#resp_input").text() );
You may use this code:
$('#responsibilities').text($("<div>" + eachline + "</div>").text() );
see this update

Categories

Resources