Add div class with php in jQuery - javascript

My variable contains a color name. My example:
<?php
$myvar = blueColour;
?>
I need to add this value to body of a html page using jQuery:
<script type="text/javascript">
jQuery(body).addClass("<?php echo $myvar; ?>");
</script>
Can I use php inside jQuery this way? Thank you.
EDIT: its an wordpress site. I use this in a if to add that class on a specific page template.

<script type="text/javascript">
jQuery("body").css("color", "<?php echo $myvar; ?>");
</script>

yes, as long as your html file is interpreted by php (having .php/.phtml extension)

I did it. Worked.
Correct is:
jQuery('body').addClass("<?php echo $myvar; ?>");
with
jQuery('body')
not
jQuery(body)

if ( is_page_template('nameOfYouTemplate.php') ) {
echo '<body class='. $myvar .'>';
} else {
echo '<body>';
}

Related

How to properly write this JS script in PHP echo?

I'd like to print this JS script.
<script src="<?php echo base_url('assets/inspinia/js/jquery-2.1.1.js'); ?>"></script>
<script src="<?php echo base_url('assets/jquery-ui/jquery-ui.js'); ?>"></script>
I write like this:
print("<script type=\'text/javascript\' src=\'".base_url('assets/inspinia/js/jquery-2.1.1.js')."\'></script>");
print("<script type=\'text/javascript\' src=\'".base_url('assets/jquery-ui/jquery-ui.js')."\'></script>");
and this:
echo "<script type=\'text/javascript\' src=\'".base_url('assets/inspinia/js/jquery-2.1.1.js')."\'></script>";
echo "<script type=\'text/javascript\' src=\'".base_url('assets/jquery-ui/jquery-ui.js')."\"></script>";
But, both method can't get the external JS file.
Note: The html in in a modal
To simplify, I will first give you an example with the url saved as a variable.
$url = base_url('assets/inspinia/js/jquery-2.1.1.js');
echo "<script src='$url'></script>";
You can keep it in the long form if you prefer, but you would need to leave the quotes and concatenate (similar to what you have been doing)
echo "<script src='" . base_url('assets/inspinia/js/jquery-2.1.1.js') . "'></script>";
For that reason, I always prefer to assign it to a variable first so that I can see the separation of "get value" and then "print string including that value"
Here is an example of using javascript in php echo:
<?php
echo '<script>Hello World!</script>';
?>
I'm a bit sorry to see what you mean, is that so?
echo "<script type='text/javascript'>
function getState(){
var cs = ".$state.";
return cs;
}
</script>";
Do you want to introduce JS files in PHP?
You can wrap yourself,and you can refer to YII2's wording, like this:
/**
* 定义按需加载JS方法
* #param $view View
* #param $jsfile
*/
public static
function addScript($view, $jsfile) {
$AssetManager = new AssetManager();
$jsfile = $AssetManager - > getPublishedUrl('#backend/modules/motorcade/assets').$jsfile;
$view - > registerJsFile($jsfile, [motorcadeUIAsset::className(), 'depends' => 'backend\modules\motorcade\assets\motorcadeUIAsset']);
}
Hope it helps you
Try this,
echo "<script src=" '. base_url('assets/inspinia/js/jquery-2.1.1.js') .' "></script>";

Pass a php variable to a jQuery function

I have searched and tried several different methods to pass a php $variable to a jQuery function. I can pass a string simply by using myfunction("Hello");. But if I try myfunction(); or myfunction($variable); with or without quotes it fails to run.
function wholesection(val) {
$("#whole-section").slideUp("fast", function () {
});
$('#label-cemetery').text("Section*");
$('#poc').val(val);
}
The above works if I send a literal string enclosed in double quotes, using:
<?php
echo '<script>',
'wholesection("Hello");',
'</script>'
;
?>
</head>
<body>
<?php
$variable = "Hello";
echo '<script>',
'wholesection(' . $variable . ');',
'</script>'
;
?>
Or other similar variants do not work.
'wholesection($variable);',
'wholesection("$variable");',
Suppose your $variable has value "Hello".
Then this code:
echo 'wholesection('.$variable.');',
is rendrered in html like
wholesection(Hello);
See? You're passing Hello to a function. Not "Hello" but Hello.
And Hello is considered a javascript variable. I bet you don't have it.
So, the fix is - add quotes:
echo 'wholesection("'.$variable.'");',
which will be rendered as:
wholesection("Hello");
You can pass it by echoing your php varriable in the script
try below code :
<?php
$variable = "Hello";
?>
<script type="text/javascript">
var simple = '<?php echo $variable; ?>';
</script>
<?php
$variable = "Hello";
if(true){
?>
<script>
wholesection("<?php echo $variable?>");
</script>
<?
}
?>

php in external javascript file

I have a the code below in my js which obviously works if the js is in the php file
filter: '<?php echo $my_cat>'
how can i make a variable maybe in the php file and pull it in my external js file?
You have to place this below code at the top part of your test.php page
<script>
var filter = '<?php echo $my_cat>';
</script>
and simply you can check it on your external.js file by alert
alert(filter);
<?php
function showme(){
echo "HelloWorld";
}
?>
<script type="text/javascript">
var my_var = '<?php echo showme(); ?>' ;
</script>
You can also use short tags.
<script>
var filter = '<?=$my_cat?>';
</script>

Parameter pass from php script to javascript function

html
<?php $test="00010387";
javascript
<script>
function browsePricebySKU(test){
alert(test);}
</script>
the output is different with the parameter. Please help me.
Try this
php code
<?php $test="00010387"; ?>
<script type="text/javascript">
function browsePricebySKU(test){
alert(test);
}
</script>
Single quotes will not be Escape, It will output $test as a string, not the value of the $test variable;
html
<?php $test="00010387";?>
(close php before starting html)
or
html
<?php $test="00010387";
echo "";

PHP Echo Inside Javascript String

I want to be able to echo a PHP link inside a JavaScript string, i.e. <?php echo SITE_URL ?>. How can I do this with the code below?
<script type="text/javascript">
$.backstretch("http://www.example.com/bg.jpg");
</script>
<script type="text/javascript">
$.backstretch('<?php echo str_replace(array("\\", "'"), array("\\\\", "\\'"), SITE_URL); ?>');
</script>
I also made sure to escape ' and \ to avoid any problems.
you can do
<script>
var test= '<?php echo SITE_URL; ?>';// and use it as you please
</script>

Categories

Resources