Issues with Javascript - javascript

I have been developing this website (First Day)
My javascript for the Beliefs section was working previously, then I changed it because the design changed a little. The javascript is the same except I needed to hide some elements when the page loaded. On a click, depending which word is clicked, that description will show up, and the others (if any were already shown) will disappear. The change I made works on my local machine, but doesn't work on my web server. What could be wrong with my javascript?
Here is the edited code; however, it is still not working
Thanks for any help. This is one I cannot figure out.
Here is the link to the zipped folder of the website. Dropbox Zipped Folder
JavaScript Code:
$(document).ready(function(){
$('.Descriptions').hide();
$('#God').click(function () {
$('.Descriptions').hide();
$('#GodDescriptions').show();
});
$('#Jesus').click(function () {
$('.Descriptions').hide();
$('#JesusDescriptions').show();
});
$('#HolySpirit').click(function () {
$('.Descriptions').hide();
$('#HolySpiritDescriptions').show();
});
$('#Bible').click(function () {
$('.Descriptions').hide();
$('#BibleDescriptions').show();
});
$('#Man').click(function () {
$('.Descriptions').hide();
$('#ManDescriptions').show();
});
$('#GodsRelationship').click(function () {
$('.Descriptions').hide();
$('#GodsRelationshipDescriptions').show();
});
$('#Salvation').click(function () {
$('.Descriptions').hide();
$('#SalvationDescriptions').show();
});
$('#SavedWho').click(function () {
$('.Descriptions').hide();
$('#SavedWhoDescriptions').show();
});
$('#Perseverance').click(function () {
$('.Descriptions').hide();
$('#PerseveranceDescriptions').show();
});
$('#GospelOrd').click(function () {
$('.Descriptions').hide();
$('#GospelOrdDescriptions').show();
});
$('#Resurrection').click(function () {
$('.Descriptions').hide();
$('#ResurrectionDescriptions').show();
});
$('#ChurchGov').click(function () {
$('.Descriptions').hide();
$('#ChurchGovDescriptions').show();
});
$('#SecondComing').click(function () {
$('.Descriptions').hide();
$('#SecondComingDescriptions').show();
});
$('#Missions').click(function () {
$('.Descriptions').hide();
$('#MissionsDescriptions').show();
});
});

Don't use IDs for toggling visibility of lots of elements. It's unmaintainable, and misses the point of what IDs are for; they're for a unique property when you need to do targeted work (navigate to them, manipulate a single element, etc).
Give all those bits a class attribute with the same class (or if they already have one, give them all the same class as extra class), and the toggle that single class.
<div id="..." class="line"> ...</div>
<div id="..." class="line"> ...</div>
<div id="..." class="line"> ...</div>
<div id="..." class="line"> ...</div>
and then a simple
$(".line").hide();
to hide everything, with
$(".line").hide();
$("#justthatline").show();
to show individual parts

Use .toggle()
This hides an object if visible and makes viable if hidden.
So in your example set things visible or hidden appropriately and then toggle the ones that should be effected by a click.
Also your comment is an HTML comment and is not valid in the context of JavaScript.
<!--Category Logic-->
should be
//Category Logic
or
/*Category Logic*/
This could be causing your problem.
Edit:
I like what mike said about using classes that would make this a million times easier to deal with and read.
Add class="description" to all you extra text and then you can use toggle to do the rest.
EX.
$(".description").hide();
$("#God").click(function(){
$(".description:not(#GodDescription)").hide();
$('#GodDescription').toggle();
});
This hides everything that isn't the description you want to toggle and then toggles that.

Related

Select multiple elements with same id - WebDriverJS

I have to create a test with Selenium, WebDriverJS and Jasmine for a function that displays the content of a div only when the button connected to that div is clicked, and hide all the other elements with the same id.
This is the function that needs to be tested, and this is a snippet of the test I have written so far:
it('should display "BILL2" when the second picture is clicked and hide the others', function() {
driver.findElement(webdriver.By.css('#img_bill2')).click()
.then(function(){
driver.findElement(webdriver.By.css('#bill2')).isDisplayed()
.then(function(displayed) {
expect(displayed).toBe(true);
});
})
.then(function() {
driver.findElement(webdriver.By.xpath('//div[#class="hide", #style="display:none"]')).isDisplayed()
.then(function(displayed) {
expect(displayed).toBe(false);
})
});
});
I am stuck here since I would like to select all the elements, except the one that is currently shown.
Do you have an idea on how to solve this issue? Maybe using executeScript() and create a script to locate all the divs with display:none style can be a solution?
Thanks in advance for your replies!
I'd prefer to solve this without using .isDisplayed() by counting the numbers of invisible elements. Since all elements would have display:none which will ensure that the elements are not displayed.
it('should display "BILL2" when the second picture is clicked and hide the others', function() {
driver.findElement(webdriver.By.css('#img_bill2')).click()
.then(function(){
driver.findElement(webdriver.By.css('#bill2')).isDisplayed()
.then(function(displayed) {
expect(displayed).toBe(true);
});
})
.then(function() {
driver.findElements(webdriver.By.xpath('//div[contains(#class, "hide") and contains(#style, "display: none")]'))
.then(function(elements) {
expect(elements.length).toBe(expectedCount);
})
});
});

how to Hide a div when css is disabled?

i have a div which is hidden initially and will be visible later depending on some click events results.
I have wrote this
$(document).ready(function () {
$('#<%=disable.ClientID %>').hide();
});
<div id="disable" runat="server">The following question is disabled</div>
But when i disable CSS it appears, when i don't disable css it gets invisible. how do i make this invisible even when css is disabled and visible later again
There is no way to make something invisible without CSS. But you can remove it:
$(document).ready(function () {
$('#<%=disable.ClientID %>').remove();
});
You would then need to readd all the mark up again should you wish to show it again.
Edit
You could do something like this:
$(document).ready(function () {
var item = $('#<%=disable.ClientID %>');
$(document).data('myElement', item.clone());
item.remove();
});
then you could re-add it
$(document).append($(document).data('myElement'));
If you are willing to write server code for this, then you could do this in the code-behind.
// c#
if(some condition...)
{
disable.Visible = false;
}
This will remove the div from the HTML output of the page.
I do not get you when talking about enabling and disabling css, but you can always manage the DOM elements via DOM manipulation. As you tagged jQuery:
$(document).ready(function () {
/* please try top avoid mix server side variables and javascript code */
$('#myTargetDiv').hide();
$('#myToggleButton').on('click',function(){
/* select the elements you want to hide / show with the click on this element */
var $elements = $('#myTargetDiv');
$elements.toggle();
});
});

Keep the hover state on the last clicked menu item

I have added a nice (jquery + css) navigation menu to my website, but there's a small problem. When I click on a menu item, the light blue box jumps back to the first item, and I would like the box to stay on the clicked item, but I have no clue how to make it happen.
Here's an example: http://jsfiddle.net/Stylock/ta8g4/
In that example, it actually works how I want, but on my website it doesn't work for some reason. Any help would be much appreciated.
You could add a class to the item like .selected
And in your css you apply the same style to the .selected than the :hover
http://api.jquery.com/addClass/
Or you cloud also modify the css using jQuery. But the first solution is more flexible.
Edit: Use the callback function to add/remove class, after your effect
This might be a solution to the right direction:
Onclick check if the menu already has a classname to get the background changed
remove that class from there
Add the class where you clicked
some minor css changes are needed and u will have solved it
have a great day
you can add a class when other page loads like to add on all pages like
this is bad one
<script type="text/javascript">
$(window).load(function () {
$("#index").toggleClass("highlight");
});
$(window).unload(function () {
$("#index").toggleClass("highlight");
});
</script>
site i used this at http://www.hoteloperaindia.com/
or like this short one to add this to master page
<script>
$(function () {
var loc = window.location.href; // returns the full URL
if (location.pathname == "/") {
$('#home').addClass('active');
}
if (/index/.test(loc)) {
$('#home').addClass('active');
}
if (/about/.test(loc)) {
$('#about').addClass('active');
}
if (/accommodation/.test(loc)) {
$('#accommodation').addClass('active');
}
if (/gallery/.test(loc)) {
$('#gallery').addClass('active');
}
if (/tariff/.test(loc)) {
$('#tariff').addClass('active');
}
if (/facilities/.test(loc)) {
$('#facilities').addClass('active');
}
if (/contact/.test(loc)) {
$('#contact').addClass('active');
}
});
</script>
site where i used this one http://rtshotel.in/
change it with your code

jQuery- dynamically alternate between image sources within an animated div

I have a menu that is animated to slide across the page when triggered by clicking on an image of an arrow. I'd like to switch the arrow to a different image source once the menu's animation has completed, and then return back to the original file when the menu has been closed.
Best way to do this?
Thank you!
HTML:
<div id="slider">
<div id="trigger_right">
<img class="arrow_small" src="images/left_small.png" alt="slide menu out" />
</div>
<div class="trans" id="overlay"></div>
<div id="content">
<p>This is content</p>
</div>
</div>
Javascript:
$(function() {
$('#trigger_right').toggle(function (){
$('#slider').animate({'width':'100%'}, 1500);
$('.arrow_small').attr('src','images/right_small.png');
}, function() {
$('#slider').animate({'width':'30px'}, 1500);
$('.arrow_small').attr('src','images/left_small.png');
});
});
jQuery's .animate has a callback that is called when the animate is finished so you can use that to change the images at the appropriate time:
$(function() {
$('#trigger_right').toggle(function () {
$('#slider').animate({'width':'100%'}, 1500, function() {
$('.arrow_small').attr('src','images/right_small.png');
});
}, function() {
$('#slider').animate({'width':'30px'}, 1500, function() {
$('.arrow_small').attr('src','images/left_small.png');
});
});
});
The above assumes that you only have one .arrow_small element of course. Using a class for the arrow and a sprite sheet for the images would be better but that would only require changing the $('.arrow_small').attr() parts to $('.arrow_small').toggleClass() calls as Rob suggests.
If I understand correctly, you only want the images to change after the menu animation has completed.
One way, perhaps not the best, would be to make the JavaScript that changes the src attribute occur after a set period of time using setTimeout(). Instead of:
$('.arrow_small').attr('src','images/right_small.png');
You would have:
setTimeout("toggleImages()", 1500);
function toggleImages(){
// some code to toggle them
}
I haven't tested this, but give it a try. Hope it helps!
I would suggest you set the images up in CSS as classes and then do something like:
.toggleClass("right-small left-small");

Jquery mouse-over fade-in/out (best practices)

I've got working Jquery code to fade in/out descriptive text in a div below the question. The problem? The solution is not very elegant. Here's what I've got:
$("#home").mouseover(function() {
$("#homeText").fadeIn("slow");
});
$("#homeText").mouseout(function() {
$("#homeText").fadeOut();
});
I know there is better way to do this, I'm just not sure what it is.
you could use hover, the first function will act on a "hover over" and the second will act on a "hover out"
The documentation is located here: http://docs.jquery.com/Events/hover
$("#home").hover(function(){
$("#homeText").fadeIn("slow");
},
function(){
$("#homeText").fadeOut();
});
How about 3 lines?
<script>
$(function () {
$('#home').hover(function() {
$('#homeText').fadeToggle("slow");
});
});
</script>
Elegant enough?
Jon, Great advice! I used as a staring point though for a more complete solution. Doing this with just the basic hover would still leave me with a hover call for single menu item..A lot of redundant code. So using what you suggested, I came up with this:
$('.topMenu').hover(function()
{
$('#_'+this.id).fadeIn("slow");
},
function ()
{
$('#_'+this.id).fadeOut();
});
});
All menu items are given the topMenu class and ID. The corresponding div to display is the same id as the menu item, just prefixed with _
Example:
....
Stuff about us!
...
Thanks!
$(function () {
$('#home').hover(function() {
$('#homeText').fadeIn("slow");
});
$('#home').mouseout(function() {
$('#homeText').fadeOut("slow");
});
});

Categories