I am passing an ID to a selector, but somehow it doesn't work. When I hardcode it, then it works. Below is my script and the various ways I have tried. :
jQuery("li.<?php echo slugify($catId); ?>").closest("ul").closest("li").addClass("active");
When I view in console, the script above echoes out the $catId correctly but it just doesn't work.
Then I tried this :-
var catId='<?php echo $catId; ?>';
jQuery("li."+catId).closest("ul").closest("li").addClass("active");
In the console, the variable catId wasn't printed out at all. It shows ("li."+catId) in the console.
Then I tried converting it to string and pass it to the selector like this :
var catString = catId.toString();
It doesn't work either. I hardcode the catId directly to the line and it works just fine.
Anyone can help? Thanks in advance.
If you are trying the above snippets your HTML should look like this
<li class="<?php echo slugify($catId); ?>"> ....
And you shouldn't be associating them to id attribute
I have managed to solve it!!!
It's just moving the whole <script></script> to after the <li> </li>
Basically the placement of the script causes the issue. :)
Related
I know the answer is probably gonna be something very simple but i'm losing my mind over not getting this to work:
<button id="buttonid<?php echo $i?>" onclick="addShift(this.id)">+</button>
<script>
function addShift(id){
alert(id);
$('#id').replaceWith( "<h2>TEST</h2>" );
}
</script>
alert shows the right ID. But i can't get the .replaceWith function to work.
$("button").replaceWith( "<h2>TEST</h2>" );
This is working by the way. What am i substantially missing here about submitting html ID's to javascript? Thanks!
I could be misunderstanding the problem, but I think it's because you are using the string '#id' instead of the value for id.
Try this:
<button id="buttonid<?php echo $i?>" onclick="addShift(this.id)">+</button>
<script>
function addShift(id){
alert(id);
$('#'+id).replaceWith( "<h2>TEST</h2>" );
}
</script>
You might be thinking that Javascript works like PHP in terms of supporting string interpolation. That means, the thing that let's you do something like $variable = 'me'; echo "$variable is cool"; in PHP, and have it print out me is cool. But Javascript doesn't do that. You need to use string concatenation instead, i.e. alert(variable + ' is cool').
I need to generate a unique ID via js or PHP. Obviously this is pretty easy, but the part I can't figure out is that I need to generate a unique id for a <p> element, and then reference that unique id in some inline js below.
Here's the code to make this more clear:
<p id="UNIQUE-ID" class="toolbox"><a class="tooltip" onmouseover="tooltip.pop(this, '##UNIQUE-ID');"></a>
Basically, in both places where it says "UNIQUE-ID", I need an identical unique id generated. There will be an unknown number of these kind of <p> and <a> pairs generated dynamically.
Any help is greatly appreciated.
You've said that the <p> and <a> pairs are generated dynamically, and also asked for a unique ID "...via js or PHP..." So I'm going to assume the dynamic generation is via PHP, in some kind of loop.
If so, just remember the value in a variable in your loop and output it in the two required locations, e.g.:
<?php
while (someCondition) {
$id = /* ...generate the ID...*/;
?>
<p id="<?=$id?>" class="toolbox"><a class="tooltip" onmouseover="tooltip.pop(this, '##<?=$id?>');"></a>
<?php
}
?>
(Obviously that's using shorttags, which many people recommend against. But it gets the idea across. If you don't use shorttags, you can easily change that to use echo instead.)
I am just giving the structure.Just change it according to your requirement
<?php
for($i=0;condition;$i++)
{
?>
<p id="par_<?php echo $i;?>" <a id="id_<?php echo $i;?>" href=""></a>
<?php
}
?>
I am trying to use a jQuery Plugin for highlighting my Snippet at This Demo but the browser assumes that
<pre class="php">
<?php
echo "My first PHP script!";
?>
</pre>
is a originally PHP code which it is! now the solution is replacing all < with < and > with >
inside the <pre class="php"></pre>
Now can you please let me know how can I use the jQuery to replace all < and > characters inside the with < and > before loading the page?
Thanks
So you got the data with <? tags, it better for you is to replace output of your code serverside with < and > but if you looking for dirty js solution it is would be:
$('.php').each(function() {
var $this=$(this);
$this.text($this.html().replace(/\<\!--\?([\s\S]*?)-->/g,'<?$1>'));
});
http://jsfiddle.net/oceog/x974w2d9/
I not remember if any browser do the replacing of the <? with <!--? but chrome does. The js solution will not work if you have some random examples like: http://jsfiddle.net/oceog/x974w2d9/2/.
Now can you please let me know how can I use the jQuery to replace all < and > characters inside the with < and > before loading the page?
The problem lies in the assumption behind this question. You cannot use jQuery to make this manipulation before the page loads, as jQuery executes after the page loads up in the browser. So your PHP is getting executed and all jQuery will see is:
<pre class="php">
My first PHP script!
</pre>
You need to make sure the string makes it into the output instead of being executed. Something like this should work:
<pre class="php">
<?php
$snippet = <<<EOT
<?php
echo "My first PHP script!";
?>
EOT;
echo $snippet;
?>
</pre>
I have looked all over this and other forums and have been banging my head on how to do this.
I have a geolocation script that uses document.getElementById("zip").innerHTML = data.zip;) to display the results into the innerhtml of something like this:
<p id="zip"></p>
NOTE: This html is all that's needed to get the results to show.
The final html results are <p id="zip">12345</p>.
NOTE: This is what shows after the results are inserted by javascript above.
I would like to know how to set "12345" (from the example above) to a php string:
$zip = 12345;
and completely remove <p id="zip"></p> from the page.
I have used str_replace to try to pull out all of the html and leave the zip, but have had no luck.
Any ideas?
Maybe just use a hidden field?
<input type='hidden' name='zip' value='12345'>
then if isset post['zip']
you get the picture
I've been working on this code for a while. The idea is to get javascript to make a div visible based on the results of a HTML form (not on this page). However, my javascript function never works. I've isolated the problem to the script not being called in the first place. Here's my code:
<!DOCTYPE HTML>
<html>
<body>
<?php
While ($result=mysql_fetch_array($data)){ //I have mySQL code before this
$equipment= $result['safety equipment'];
$equipment2= str_replace(' ', '_', $equipment); //modified equipment name without spaces so that post can read it
$problem = $_POST[$equipment2];
?>
<div style="display:none;" id="<?php echo $equipment?>"> <!--Code that creates a div for each equipment -->
<h1>Report a problem</h1> <br>
You reported that there is a problem with the <?php echo $equipment." in ".$name;?>.<br>
Please describe the problem.
<form>
<textarea row="5" column="300" name="Issue">
</textarea>
</form>
</div>
<?php
if ($problem=="working"){
inspectRoom($UID,$equipment,null);
}else {
echo $equipment; //this part works
echo'<script type="text/javascript">'; //this part does not work
echo'console.log("test");';
echo'var test ='.$equipment.';';
echo'alert (test);';
echo'Appear(test);';
echo'</script>';
}
}
?>
<script type="text/javascript">
function Appear(equipment){
alert("hi"); //error trapping
document.getElementById(equipment).style.display='block';
}
</script>
</body>
</html>
$equipment is a string with the name of the equipment (ex: Fume Hood)
$problem is a string retrieved from the previous page. It too has the name of some equipments.
In my console I get the following error:
"SyntaxError: missing ; before statement"
What am I doing wrong?
You're dumping PHP-based text directly into a Javascript context, which is highly dangerous. Any JS metacharacters (especially ') will cause syntax errors and kill the entire JS code block.
echo'var test ='.$equipment.';';
Should be
echo 'var test = ', json_encode($equipment), This will produce syntactically valid JS code, no matter what's in `$equipment`.
Plus, you have MANY other syntax errors. Your php while is NOT contained in a <?php ... ?> code block, so it'll appear directly in your output as raw text. The html inside your while loop is now considered part of the PHP code, so that'll be yet another syntax error. etc... etc... etc.. In other words this code is utterly broken.
Talking solely about javascript since that's what your question is about, your Appear function would never been defined yet since the PHP code would have echoed out the JS that calls Appear before the function is defined.
It is not working because you are forgetting the space after echo. There are other errors also such as your while is not within tags.
For this part of the code
echo $equipment; //this part works
echo'<script type="text/javascript">'; //this part does not work
echo'console.log("test");';
echo'var test ='.$equipment.';';
echo'alert (test);';
echo'Appear(test);';
echo'</script>';
Replace it with
echo $equipment; //this part works
echo'<script type="text/javascript">'; //this part does not work
echo'console.log("test");';
echo"var test ='$equipment';";
echo'alert (test);';
echo'Appear(test);';
echo'</script>';
Th variable isn't surrounded by quotes when the value will be a string;
To start with you are missing a > on line 6.