Cant run any JS related thing. MVC page - javascript

#model TripGMap.Models.PhotoModel.Photo
<div>
<dl class="dl-horizontal">
<a id="single_1" href="http://farm4.staticflickr.com/3712/9032543579_1217e6566b_b.jpg" title="Singapore from the air (Andrew Tan 2011)">
<img src="http://farm4.staticflickr.com/3712/9032543579_1217e6566b_m.jpg" alt="" />
</a>
</dl>
</div>
#section scripts
{
<script>
$(document).ready(function() {
$("#single_1").fancybox({
helpers: {
title: {
type: 'float'
}
}
});
});
</script>
}
This is my View. I cant run any js here. As you can see i am trying to run a fancybox on my image but it's not working. It's just sending me to the image source ;/. What am i doing wrong?
I already tried to delete #section scripts thing and so on. Still JS doesnt work. I even added some connections on the View.
Any suggestions? What's wrong? What am i missing?
Thanks for any advice!

Is your js code even outputted in HTML source?
Look at the console, is there an error? If so it should tell you what's wrong.
You included jQuery before or after your code? If after, your code will not work since jQuery is not even initialized when you try to access it via $.

Related

Trying to load HTML into a div with a button

I've spent enough hours googling this to feel comfortable posting this- even though i'm sure it's a simple solution. I'm just getting into a webdev so pardon my ignorance. As the title says i'm simply trying to have the content of an HTML file appear in a div after a button is pressed. Here is my code:
HTML:
<button id="button" class="linkGeneration">Press me</button>
<div id="renderArea" class="contentArea">
<!-- CONTENT SHOULD GENERATE HERE -->
</div>
Javascript:
$(document).ready(function() {
$('button').click(function(e) {
e.preventDefault();
$("#renderArea").load($(this).attr('../html/content.html'));
});
});
I'm not getting any errors, the button simply does nothing. I do have Jquery properly applied in my HTML as well.
Any suggestions on how to fix this OR a different method that might be simpler? Thanks.
The button doesn't have an attribute named ../html/content.html, so $(this).attr('../html/content.html') is not returning anything.
If you want to load from the URL, just use that as the argument to .load(), you don't need .attr().
$(document).ready(function() {
$('button').click(function(e) {
e.preventDefault();
$("#renderArea").load('../html/content.html');
});
});

Embarrassed that I can't get fancybox working

MVC 3 and fancybox 2.
Should be straight forward but somehow I have made a mess of things.
First since I am using this only on one page (cshtml) I add the scripts via:
Html.AddScriptParts(#Url.Content("~/Scripts/fancybox2/source/jquery.fancybox.pack.js?v=2.1.5"));
Html.AddCssFileParts(#Url.Content("~/Scripts/fancybox2/source/jquery.fancybox.css?v=2.1.5"));
When I look in the debugger I see jquery.fancybox.pzck.js loaded. When I inspect the element I do not see the jquery.fancybox.css styles applied...(css is default so maybe there aren't any to apply?)
My onready is:
$(function ()
{
$(".fancybox").fancybox();
});
and my link is:
<div id="ThreadLink">
<a id="ForumFrameLink" href="https://www.google.com" class="fancybox">asdf</a>
</div>
It is acting as though my onready is not attaching...in other words it just follows the link to Google.
What am I missing?
TIA
In the JSFiddle link you provided: http://jsfiddle.net/ramjet/54kag/
Use the following JS:
$(document).ready(function() {
$("#ThreadLink a").fancybox({
type: "iframe"
});
});
And then your HTML simply becomes:
<div id="ThreadLink">
<a id="ForumFrameLink" href="http://www.foxnews.com">asdf</a>
</div>
It seems that type: "iframe" needed to be set. After glancing over the documentation, it looks like you can also just set the class of the a to iframe instead to get the same functionality.
"Corrected" fiddle for your viewing pleasure: http://jsfiddle.net/54kag/1/

How do I get an img click to execute js using jsfiddle

I need an image click to execute a js function. I have it working in my project, but I have other code to develop so I'm trying to use jsfiddle. But I can't get this simple code to work in jsfiddle. when I use jsfiddle and I click the image, nothing happens.
What am I missing in my efforts to use jsfiddle?
My HTML
<div>
<img id="additem" Title="Add Item" onclick="myfunc()" OnMouseOver="this.style.cursor='pointer';" OnMouseOut="this.style.cursor='default';" src="~/images/Sign_Add_Icon_32.png" />
</div>
My JS
function myfunc() {
alert("hello");
}
Remove onload in framework and extensions, set it to in head or in body.
DEMO
This works! But what was the problem?
You can't wait for the document to load in that case, because myfunc isn't defined before the Javascript is executed after the HTML loads.

My div show/ hide code works fine on a static HTML page but doesn't work on a Wordpress 3.5.1 page

This is sort of a condensed version of the code, the real version is too long to post but this is enough to represent the concept. I am using this to switch guitar diagrams based on several choices represented by anchors with the corresponding id in the href="". After spending several days getting it to work just right on a static html page, the script won't work in a Wordpress page which is where I intend to use it. I have tried it with the script in the head or inline (which shouldn't matter) - but either way it will not function. I know that Wordpress and certain plugins use Jquery so there may be a version mismatch causing conflicts. I am not (yet) an expert in javascript but I know there are several ways to skin a cat as the saying goes, I just need to find one that plays nice with Wordpress. Any help would be greatly appreciated...
<script src="http://code.jquery.com/jquery-1.8.3.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
var divswitch = $('div.diagram_container a');
divswitch.bind('click',function(event){
var $anchor = $(this);
var ids = divswitch.each(function(){
$($(this).attr('href')).hide();
});
$($anchor.attr('href')).show();
event.preventDefault();
});
});
</script>
<style>
.diagram {
margin: 0;
width: 100%;
}
.diagram_container {
width: 100%;
}
</style>
<div id="RH_RW_Div" class="diagram_container" style="float:left; display:block;">
<div class="diagram_menu">
<a class="checked" href="#RH_RW_Div"><span class="checkbox_label">Right Handed</span></a>
<a class="unchecked" href="#LH_RW_Div"><span class="checkbox_label">Left Handed</span></a>
</div>
<img class="diagram" src='images/RH_RW.gif' /><br />
</div>
<div id="LH_RW_Div" class="diagram_container" style="float:left; display:none;">
<div class="diagram_menu">
<a class="unchecked" href="#RH_RW_Div"><span class="checkbox_label">Right Handed</span></a>
<a class="checked" href="#LH_RW_Div"><span class="checkbox_label">Left Handed</span></a>
</div>
<img class="diagram" src='images/LH_RW.gif' /><br />
</div>
Wordpress uses by default jQuery.noConflict(). This is to assure that there is no conflict by other libraries using the $ variable. That's why your console says it's not a function.
However, obviously, the jQuery variable still works, and you should use that, and passing to your function the $ variable yourself to enable the shorthand version of jQuery.
So your code should look like this:
jQuery(document).ready(function($){
// Your functions go here
});
My guess is that your Wordpress install or a plugin is already loading up jQuery in the head. Check to see if it exists there, and if it does, don't call it again.
If that doesn't do it and you have this site online, send me the link and I'll take a look.
Calling jQuery twice will often lead to problems. There is also a proper way to load jQuery and override the Wordpress version if you specifically need 1.8.3 (wp_register_script and wp_enqueue_script), but I don't think you need to go down that route yet.

Auto load fancybox using .trigger('click')

I was just wondering if you could please help. I am trying to get the
fancybox to load automatically once the page is rendered. However, I
got this error message 't is undefined' at line 18 (/js/fancybox/
jquery.fancybox-1.3.3.pack.js). At the moment, I am using jquery
version 1.4.2
Click me
<div style="display:none">
<div id="container">Fancybox Content Here .... </div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('#profile').fancybox().trigger('click');
});
</script>
The funny thing is if I take the line below and run it in firebug
console then it WORKS!. Not sure what did I do wrong here. Your helps
would be grateful.
$('#profile').fancybox().trigger('click');
Thanks
I got the same problem. Testing with the unpacked version, i got "loading is undefined" in line 36 (fancybox 1.3.4). So I called "$.fancybox.init();" before triggering and it works correctly now.
Nope th reason is because the fancybox.js is included twice this is the file that has the core of fancy box
I've managed with the similar case in the way like this:
$(".some_wrapper").on('click', function(){
var lnk = $(this).find("a").first();
if (hr && ~cls.indexOf("fancybox-a")) {
$.fancybox(lnk);
}
});

Categories