I can't get the alert to fire. What am I missing?
(the code below is inside a WP plugin)
<?php
wp_enqueue_script('jquery');
?>
<script type="text/javascript">
jQuery('#myTest').click(function(){alert('hi');});
</script>
<?php
echo "<div><input type='button' value='Test' id='myTest' /></div>";
?>
The script is processed before the element, so #myTest doesn't exist when you're assigning the handler. Swap them around:
<?php
echo "<div><input type='button' value='Test' id='myTest' /></div>";
?>
<script type="text/javascript">
jQuery('#myTest').click(function(){alert('hi');}));
</script>
Alternatively, you can use live() which allows binding events to elements that don't exist yet:
<script type="text/javascript">
jQuery('#myTest').live("click", function(){alert('hi');}));
</script>
<?php
echo "<div><input type='button' value='Test' id='myTest' /></div>";
?>
Or make use of jQuery's ready() handler, which will fire when all elements have been parsed:
<script type="text/javascript">
jQuery(document).ready(function ($) {
$('#myTest').click(function(){alert('hi');}));
});
</script>
<?php
echo "<div><input type='button' value='Test' id='myTest' /></div>";
?>
Note that in the last example, I'm aliasing the jQuery namespace in the $ argument that is passed to the function. This allows you to use $ for jQuery with WordPress instead of typing out jQuery all the time.
You're missing the document.ready handler, like this:
<script type="text/javascript">
jQuery(function() {
jQuery('#myTest').click(function(){alert('hi');});
});
</script>
By wrapping it like this, it'll wait until the element's loaded in the DOM and ready to be found by jQuery('#myTest').
The script executes before the content is loaded. When the script executes it is unable to find an element with an ID of myTest so it just ignores adding the event handler.
Either put the script after the event, or listen for an ( onload | document.ready ) event.
try this in your script tags:
$(document).ready(function() {
$('#myTest').click(function(){alert('hi');});
});
Related
I want to select a user name and print something once the name is selected. However, why is "load_new_content()" not defined?
<html>
<head>
<title>Profile Editor</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js">
$(document).ready(function() {
$("#selectedUserId").change(load_new_content());
});
function load_new_content() {
alert("hello");
}
</script>
</head>
<body>
<?php
error_reporting(-1);
require('Db.php');
$db = new Db();
// var_dump($db);
$userIds = $db -> getUserIds();
echo '<select name="selectedUserId" id="selectedUserId" onchange="load_new_content()">';
echo '<option value="">Choose your CEC User Id</option>';
foreach($userIds as $ID) {
echo "<option>".$ID["user_id"]."</option>";
// echo $userId["user_id"];
// echo $ID['user_id'];
}
echo '</select>';
?>
Wrong <script> tag declaration.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <!-- Need this close tag -->
<script> <!-- Need a Open tag -->
$(document).ready(function() {
$("#selectedUserId").change(load_new_content());
});
function load_new_content() {
alert("hello");
}
</script>
Two problems. First one noted by others, you need a separate script tag for your inline JavaScript.
Second, you can't just do
$("#selectedUserId").change(load_new_content());
This will just execute the function once when the page loads.
You need to do it like this:
$("#selectedUserId").change(function() { load_new_content() });
(note the function call within another function)
or this:
$("#selectedUserId").change(load_new_content);
(note the lack of parenthesis)
This can be solved by wrapping the call to load_new_content in a function:
$("#selectedUserId").change(function(){ load_new_content(); });
Also, you are not closing the <script> tag that calls jQuery, or opening a new <script> tag for the subsequent code. Try:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#selectedUserId").change(load_new_content());
});
function load_new_content() {
alert("hello");
}
</script>
I have the following code...
PHP/HTML
<?php
if (isset($sent_notification)) {
echo "
<div id='notifyy'>
Your message has been sent!
</div>
<script>
document.getElementById('message-header').scrollIntoView();
</script>
";
}
?>
JavaScript:
<script type='text/javascript'>
$( '#notifyy' ).show(function(){
$(this).fadeOut(5000);
});
</script>
This is for a function which sends an email. $sent_notification is set to one if the email sends, which is why I can't just use a GET variable in the form to trigger the JS. However, using the exact same script on the same page with a GET variable in place of $sent_notification, the DIV fades out nicely. Why is the email notification DIV failing to fadeout?
What about putting it into a document ready function?
and chain the show and fadeout function?
$("#test").show().fadeOut(5000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="test">Fade element</div>
I am trying to load a table dynamically in a div in a php page. I want to load the tables using buttons or links on the same page.
I tried this:
<button id="fcluns" type="button" onclick="loadpage(this.id)">FC LUNs</button>
<div id="Table"></div>
<script type="text/javascript">
function loadpage(clicked_id){
if (clicked_id =="fcluns"){
$("Table").html('loadingImage.gif').load('getfcLuns.php?name=<?php $Host=$_GET['name']; echo $Host;?>');
}
}
</script>
i am generating the table from a php page getfcLuns.php where mysql queries are executed to get the details from database. And i am getting the $host in this php and passing it to the helper php to generate the table.
Any other efficient way is welcome. But I want to keep the page php based only.
<script src="ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"; type="text/javascript"> </script>
<button id="fcluns" type="button" onclick="loadpage(this.id)">FC LUNs</button>
<div id="Table"></div>
<script type="text/javascript">
function loadpage(clicked_id){
if (clicked_id =="fcluns"){
$("Table").load("getfcLuns.php?name=<?php $Host=$_GET['name']; echo $Host; ?>");
}
}
</script>
This above script Worked.
Try
$("#Table").css({'background-image':'loadingImage.gif'}).load(
'getfcLuns.php?name=<?php echo $_GET[name];?>',
function() {
$(this).css({'background-image':'none'});
}
);
Read the Documentation http://api.jquery.com/load/
I'm working on a project that uses jQuery modals that dynamically load their content on a button click.
I am having an issue using this loaded content like I would normally.
Here is an example of my problem
<script type="text/javascript>
click function{
load modal{
open: $('#modalID').load('phpfile.php?id=<?php echo $id ?>');
}
</script>
That all works fine, but when trying to use jQuery within the "phpfile.php" is where the problem lies
<?php
$id = $_GET['id'];
$db = USE ID TO GET DATABASE INFO; //works fine here
?>
//ECHO SOME HTML HERE
<script type="text/javascript">
$('#buttonID').on('click', function(){
alert('test <?php echo $id ?>');
});
</script>
When I click the button, I get the alert but it just says test and I don't get the ID like I should.
Thanks in advance for your help and suggestions on this one!
You have pasted pseudo code so it is difficult to say what is wrong in your code.
The following should work, give it a try:
File 1.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<?
$id = "10";
?>
<script>
$(document).ready(
function(){
$('#modalID').load('phpfile.php?id=<?php echo $id ?>');
});
</script>
<div id = 'modalID'></div>
And phpfile.php:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<?php
$id = $_GET['id'];
?>
<script>
$('#buttonID').on('click', function(){
alert('test <?php echo $id ?>');
});
</script>
<input type="button" id = "buttonID" value="click me">
Please check on the do you have any errors in your browser's console. The above code should work. The possibilities are like if you don't get your $_GET['id'] or any script errors will prevent execution of your code.
I have created a sample php file which is working fine.
<?php
$id = rand();
?>
<button id="buttonID"> Click </button>
//ECHO SOME HTML HERE
<script type="text/javascript">
$('#buttonID').on('click', function(){
alert('test <?php echo $id ?>');
});
</script>
I am creating a PHP App and am using JQuery but have run into problems. For example, below is the code for a page i am writing, the alert() function calls when the pages loads but the second javascript Jquery code does not, I cannot see why one line works but the second does not?
help.php
<?php
echo "<script>";
echo "alert(\"Loaded\");";
echo "$(\"#newDiv\").draggable().resizable();";
echo "</script>";
echo "<div id=\"newDiv\">";
echo "</div>";
?>
The page calls the alert and creates the div but does not execute the jquery statement.
I think this may be due to have my page is structures and loaded. pages are loaded inside a div using Jquery and AJAX so the only page the user actually "loads" is an index.php page like so:
index.php
<html>
<head>
<title>BluePrintr Web Application.</title>
<link rel="stylesheet" type="text/css" href="public.css" />
<script type="text/javascript" language="javascript" src="jquery.js"></script>
<script type="text/javascript" language="javascript" src="myLibs.js"></script>
</head>
<body>
<?php
echo "<div id=\"web_Page\">";
//////////////////////////////////////////////////////
echo "<div id=\"web_Header\">";
require("public/templates/header.php");
echo "</div>";
//////////////////////////////////////////////////////
echo "<div id=\"web_Menu\" class=\"horizontalcssmenu\">";
require("public/templates/menu.php");
echo "</div>";
//////////////////////////////////////////////////////
echo "<div id=\"web_Content\">";
echo "</div>";
//////////////////////////////////////////////////////
echo "<div id=\"web_Footer\">";
require("public/templates/footer.php");
echo "</div>";
echo "</div>";
?>
The menu loads and then any page selected from this menu is then loaded into the "#web_Content" div. So when the help.php page is selected from the menu, it injects the script into the div.
Can anyone see why the javascript code will not execute on help.php?
You need to wrap the jquery to initialize only after the page loads
<?php
echo "<script type='text/javascript'>";
echo "alert(\"Loaded\");";
echo "$(function(){$(\"#newDiv\").draggable().resizable();});";
echo "</script>";
echo "<div id=\"newDiv\">";
echo "</div>";
?>`
The $(function(){}); will cause the code to wait until after the page has been completely loaded.. I'm assuming you are also loading jQuery UI as you are using .draggable()?
Do this:
<script type='javascript'>
$(document).ready (function() {
alert('Hi');
// Other initialization
});
</script>
Also, just write all of that as HTML. Use PHP like this:
<html>
<body>
<?php if ($var == true) {?>
<p>Hello</p>
<?php } ?>
</body>
</html>
If $var is false, the page is blank. This makes things much more readable.
your javascript may be executing before the page has finished rendering in the browser
have a look at the jquery ready function for examples
http://api.jquery.com/ready/
As mentioned, you need to wrap your jQuery code inside $(document).ready().
You should stop doing it the way you are now and use a templating system, with a proper MVC architecture for serving your pages. It's just gonna be hell for you if you keep doing it this way. You should never have PHP outputting javascript code, this should be written by you in seperate files in a functional way and then included in your HTML file.
There is another good way like below:
$alert = <<<LABEL
<script>
$(document).ready (function() {
alert('Hi');
// Other initialization
});
</script>
LABEL;
echo $alert;