jquery ready event not working in html - javascript

I am new to javascript and I have no idea why this code is not working in my html document. I am trying to get an alert after my document is ready. I've checked and it seems syntactically correct to me. the script is at the top of my html document. Any help is appreciated!
It works if it is invoked directly like so:
<script language="javascript" type="text/javascript">
$(function(){
alert("My First Jquery Test");
});
</script>
but doesn't work when trying to invoke after document is ready:
<script language="javascript" type="text/javascript">
function test1(){
alert("My First Jquery Test");
}
$(document).ready(function(){
test1();
});
</script>

Bracket / parentheses problem:
Last line should read });
If you pop open your dev tools (F12 most browsers) you will probably see a parse error in the console.

Change
$(document).ready(function(){
test1();
)};
to
$(document).ready(function(){
test1();
});

You need to make sure that jQuery is loaded. Otherwise,
$(document).ready(function(){
test1();
});
will not work. If you add
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>‌
as the first line of your code, jQuery will load from Google's hosted library and your code should work properly.

Related

How to use unobtrusive JavaScript approach to call a function using anchor tag?

There are number of questions on this and I tried them out. However I am having an issue with the following anchor tag which acts as a button. Why isn't the link click event calling the function?
try 1:
Submit
try 2:
Submit
js code:
<script>
$("#btnSubmit").click(function(){
dbdata = <%=jsscripts()%>;
addAll(0, this);
});
</script>
<script type="text/javascript" src="cart_Test.js"> </script>
NOTE:
Please note that this particular js function works well when it's called under the DOM load event listener.
UPDATE:
dbdata is an array and addall() is a function defined in the cart_Test.js file. It seems, following script is not fired after the click function event.
<script type="text/javascript" src="cart_Test.js"> </script>
The order of the js:
<script>
document.addEventListener("DOMContentLoaded", theDomHasLoaded, false);
function theDomHasLoaded(e) {
//datepicker stuff
}
</script>
<script>
$("#btnSubmit").click(function(){
});
<script type="text/javascript" src="cart_Test.js"> </script>
Well, I am adding an answer since this is how I got it solved. It could be the order of execution.
There was a script snippet for a jQuery UI datepicker. Somehow it was blocking above stated scripts which are supposed to be the first scripts to be run in this page.
After removing the datepicker script (it can be any other script, not necessary a datepicker.) and adjusted the order of execution, the programme flow was back to normal and all scripts were firing as expected.

How can I link JQuery AND another JS file to my html?

I need some help linking my JS file to my html file after I linked jQuery from Google CDN. I could do and put the code inside, but it makes my code look untidy.
This is what I'm doing:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</script>
<script src="images/script.js"></script>
<script src="script.js"></script>
After that, my code does not work. However, when I put it into script tags, it does. I'm linking to script.js correctly, but the code doesn't activate when I try clicking on something. What can I do to fix this?
The code I created was:
$('div').on('click', function () {
$(this).toggleClass('show-description');
});
Thanks!
Try putting your code within document ready function from jQuery. The issue is your DOM is not ready at the time you defined the onclick handler.
jquery document ready function
$( document ).ready(function() {
$('div').on('click', function() {
$(this).toggleClass('show-description');
});
});
Check your file pathway for your script.js file in regards to your html file.

Hide Image when another image is clicked

this seems to be simple.. but I am a bit noobish with jquery, maybe I am doing something silly wrong?
I want to click an image, and on that click, hide another image right next to it.
<script type="text/javascript">
$("#butShowMeSomeUnits").click(function() {
$('#arrowUnitspic').hide();
});
</script>
Id's are correct as per the two images. What am I missing? Debugging it, the code never gets fired...
Thanks
EDIT
I had my control as a nested control on an asp masterpage, and its id was being rewritten. I have now fixed the id, but I still cant get any joy... I also see my markup is being rendered as an "input", would that make a difference?
<head>
<script src="js/jquery.min.1.5.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#butShowMeSomeUnits").click(function () {
$('#arrowUnitspic').hide();
});
});
</script>
</head>
<body>
<input type="image" src="bookings_media/buttons/show-me-some-units.png" onmouseout="this.src='bookings_media/buttons/show-me-some-units.png'" onmouseover="this.src='bookings_media/buttons/show-me-some-units_orange.png'" id="butShowMeSomeUnits" name="ctl00$ctl00$ContentPlaceHolder1$bookings_right_content$butShowMeSomeUnits">
</body>
EDIT
JS Fiddle
If there is any confusion... the JS fiddle I spooled up with the exact code also does not work...
You need to do do on page ready:
<script type="text/javascript">
$(document).ready(function() {
$("#butShowMeSomeUnits").click(function() {
$('#arrowUnitspic').hide();
});
});
</script>
Edit:
The fiddle you provided did not work until I chose jQuery 1.10.1 from the dropdown. You will notice your onmouseover changes the element first, but once you click on the input it does hide the image. Can you verify this works the same for you?
If the answer is no then I don't think you are loading the jQuery library on your page. To check this should work:
if (typeof jQuery != 'undefined') {
alert("jQuery library is loaded!");
}else{
alert("jQuery library is not found!");
}
In addition it might be helpful to see what errors your browser console /dev tools is showing.
Wrap the code in jQuery.ready() event. And also check whether jquery js file is loaded or not.
$(document).ready(function(){
$("#butShowMeSomeUnits").click(function() {
$('#arrowUnitspic').hide();
});
});
You code looks good and works check here
What you might be missisng is either:
to load jQuery script in the head of your page.
to include $(document).ready(function() { //code here }); in case your <img> tags are after the script in the page code. This is so your code loads when page is ready/loaded.
Steps that may help you:
make sure you integrate jQuery lib right. (to check that you might wanna open console on chrome and type $("html").hide(); and see if the current page dissapears)
make sure your custom JS file or code is UNDER the including of jQuery lib.
very good starting point with jQuery is to put everything in $(document).ready() as below example:
$(document).ready(function(){
$("img").click(function(){
$("img").hide();
$(this).show();
});
});

jQuery conflict when removing div after page load

I'm trying to remove a div from the page (preferably prevent it from loading at all)
but for now I'm settling on removing it after the page loads.
When I try the following lines of code in jsFiddle, the #content div gets removed, as expected.
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$('#content').remove();
});//]]>
</script>
However, I have also tried implementing it on an actual website, but in that case, the #content div isn't removed.
Any suggestions as to what might be wrong?
If you're sharing jQuery with another library that uses the dollar for its operation you need to guard against it like this, using an anonymous wrapper:
(function($) {
$(window).on('load', function(){
$('#content').remove();
});
}(jQuery));
Note that instead of .load() I'm using .on('load', fn).
Instead of on page load you could also bind your code on DOM ready; jQuery passes itself as the first argument to the inner function:
jQuery(function($) {
$('#content').remove();
});
Your $ variable does not point to jQuery for some reason.
<script type='text/javascript'>
window.$ = jQuery;
$(window).load(function()
{
$('#content').remove();
});
</script>
Try with this
<script type='text/javascript'>
$(document).ready(function(){
$('#content').remove();
});
</script>
or
$(function()
{
$('#content').remove();
});
It's because you have a javascript error when you call $(window).load().
Uncaught TypeError: Object [object global] has no method 'load'
Also, you should better use document.ready instead as the content will be removed faster (doesn't need to wait for all the images to load).
//shorthand for $(document).ready()
$(function(){
$('#content').remove();
});
TypeError: $(...).load is not a function
It is giving error above instead of below one as load is deprecated in new version of Jquery
$(window).load(function(){
});
use this code
$(function(){
// your code here
})
use this:
<script type="text/javascript">
var node = document.getElementById('content');
if(node.parentNode)
{
node.parentNode.removeChild(node);
}
</script>
Hope this help.
Case of jquery conflict. You have mootools framework too on the page.
Also I did a 'view source' of the page and I found out this line
$j = jQuery.noConflict(); //line 132
So try this
$j('#content').remove();
Or
jQuery('#content').remove();
You are using jQuery testing with onload,
so you have to add onload syntax in jquery, on your site the statement was not called onload, that why its not working
I have updated fiddle
http://jsfiddle.net/MarmeeK/FRYsJ/3/
The JS code under <script> tag, without adding in onload in page,
<script type="text/javascript">
$(document).ready(function(){$('#content').remove();});
</script>
this should work :)

javascript placed inside an usercontrol loads but does not execute

inside a user control I have placed:
<script type="text/javascript">
$(function () {
alert("foooo");
};
</script>
This javascript loads into the browser fine, but does not executes. What is the proper way to add javascript code to a user control in ASP.NET MVC.
I have no experience with asp.net but the above is invalid JavaScript (no closing right parenthesis). Have you tried some very simple code like this:
<script type="text/javascript">
alert("foo");
</script>
You're missing a close paren at the end of your function. It should be like this:
});
Be sure that you have JQuery library referenced. Check it like with below code :
<script type="text/javascript">
if(JQuery == "undefined") {
alert("you didn't referenced the JQuery properly!")
}
$(function () {
alert("foooo");
});
</script>

Categories