Dropdown menu doesn't work with JavaScript - javascript

I tried to make a dropdown menu with a JavaScript because the html gets generated. So I got help from some users but it's not working properly yet.
This is my menu now:https://jsfiddle.net/rxLg0bo4/5/
As you can see in the jsFiddle the menu doesn't disappear when I leave the area.
And it should also change, when I go from menu1 to menu2.
As you can see, I don't really have a smart structure for building a dropdown menu, but it should work as well. I'm working with ASP.NET so this code gets generated.
<div id="wrapper_menu">
<div id="menuicon">
<div class="menubar" id="menubar-top"></div>
<div class="menubar" id="menubar-mid"></div>
<div class="menubar" id="menubar-bottom"></div>
</div>
<nav id="menu">
<div id="pnlMenu"> <a class="menu_link" href="Index.aspx?category=1">menu1</a><a class="menu_link" href="Index.aspx?category=2">menu2</a><a class="menu_link active_menu_link" href="Index.aspx?category=4">menu3</a><a class="menu_link" href="Index.aspx?category=5">menu4</a><a class="menu_link" href="Index.aspx?category=6">menu5</a><a class="menu_link last_menu_link" href="Index.aspx?category=8">menu6</a>
</div>
<div id="pnlSubmenu" style="display:none">
<div class="submenu_panel" style="height:100px"> <a class="submenu_link" href="Pages/Chart.aspx?id=7">submenu1</a><a class="submenu_link" href="Pages/Chart.aspx?id=8">submenu2</a>
</div>
<div class="submenu_panel" style="height:100px"> <a class="submenu_link" href="Pages/Chart.aspx?id=4">Link1</a>
<a class="submenu_link" href="Pages/Chart.aspx?id=11">Link2</a>
</div>
</div>
</nav>
</div>
This is my JavaScript that I have now:
$("#pnlMenu .menu_link").mouseover(function () {
$(".submenu_panel").css("height", "100px");
$("#pnlSubmenu").slideToggle('fast');
});
That's my aspx code, as you can see I already added the jquery to it:
<head runat="server">
<title> 2.0 Preview</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta charset="UTF-8">
<link rel="stylesheet" href="Styles/font-awesome-4.2.0/css/font-awesome.min.css">
<link rel="stylesheet" href="Styles/StyleSheet.css" />
<link rel="shortcut icon" href="Images/favicon.png" type="image/x-icon" />
<!-- Scripts -->
<script src="/Scripts/auto-rotate.js"></script>
<script src="/Scripts/jquery.min.js"></script>
<script src="/Scripts/key-nav.js"></script>
<script type="text/javascript" src="/scripts/jquery.min.js"></script>
<asp:ContentPlaceHolder ID="HeadContent" runat="server"></asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<header>
<nav>
<asp:HyperLink ID="lnkLogin" NavigateUrl="~/Pages/Account/Login.aspx" runat="server">Login</asp:HyperLink>
<asp:LinkButton ID="lnkLogout" runat="server" OnClick="lnkLogout_Click">Logout</asp:LinkButton>
</nav>
<nav>
<asp:Label ID="litStatus" runat="server" Text=""></asp:Label>
</nav>
</header>
<div id="wrapper_secondary">
<div id="wrapper_logo">
<a href="/Index.aspx">
<div id="logo"></div>
</a>
</div>
<div id="wrapper_headermenu">
Management
<asp:HyperLink ID="lnkRegister" NavigateUrl="~/Pages/Account/Register.aspx" CssClass="header_hyperlink" runat="server">Register</asp:HyperLink>
Kontakt
<a href="#">
<label>
Fullscreen
<asp:CheckBox id="fullscreen_chbx" OnCheckedChanged="fullscreen_chbx_Click" runat="server" AutoPostBack="true"/></label>
</a>
</div>
</div>
<div id="wrapper_menu">
<div id="menuicon">
<div class="menubar" id="menubar-top"></div>
<div class="menubar" id="menubar-mid"></div>
<div class="menubar" id="menubar-bottom"></div>
</div>
<nav id="menu">
<script type="text/javascript">
$('.menu_link').hover(function () {
$("#pnlSubmenu").slideDown('slow');
});
$('#pnlSubmenu').on("mouseenter", function () {
$(this).show();
});
$('#pnlSubmenu').mouseleave(function () {
$(this).hide();
});
$('.menu_link').mouseleave(function () {
$("#pnlSubmenu").hide();
});
</script>
<asp:Panel ID="pnlMenu" runat="server"></asp:Panel>
<asp:Panel ID="pnlSubmenu" runat="server">
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</asp:Panel>
</nav>
</div>

When using descendant selector, you can not use id selector for the first element.
css like below will not work.
#pnlMenu .menu_link:hover #pnlSubmenu .submenu_link {
height:50px;
display:block;
}

first, I have to mention that you can use the bootstrap dropdown it will make your life easier, second I came up with a couple of changes you can make, it's not perfect, but it will get you there:
HTML
<div id="wrapper_menu">
<div id="menuicon">
<div class="menubar" id="menubar-top"></div>
<div class="menubar" id="menubar-mid"></div>
<div class="menubar" id="menubar-bottom"></div>
</div>
<nav id="menu">
<div id="pnlMenu"> <a class="link1 menu_link" href="Index.aspx?category=1">menu1</a><a class="menu_link" href="Index.aspx?category=2">menu2</a><a class="link3 menu_link active_menu_link" href="Index.aspx?category=4">menu3</a><a class="menu_link" href="Index.aspx?category=5">menu4</a><a class="menu_link" href="Index.aspx?category=6">menu5</a><a class="menu_link last_menu_link" href="Index.aspx?category=8">menu6</a>
</div>
<div id="pnlSubmenu" style="display:none">
<div id="link1" class="submenu_panel" style="height:0px"> <a class="submenu_link" href="Pages/Chart.aspx?id=7">submenu1</a><a class="submenu_link" href="Pages/Chart.aspx?id=8">submenu2</a>
</div>
<div id="link3" class="submenu_panel" style="height:0px"> <a class="submenu_link" href="Pages/Chart.aspx?id=4">Link1</a>
<a class="submenu_link" href="Pages/Chart.aspx?id=11">Link2</a>
</div>
</div>
</nav>
</div>
you can see here that I added class link1 and link3 to two menu links, you should fill the rest, and added the same values as id for the dropdown part, then on the mouse over event I will read this value and call the appropriate id to show.
and as for why the menu wasn't disappearing, you need a mouseout event as follows:
$("#pnlMenu").mouseover(function (event) {
var x = $(event.target).attr('class').split(' ')[0];
console.log(x)
$("#"+x).css("height", "100px");
$("#pnlSubmenu").slideToggle('fast');
});
$("#pnlMenu").mouseout(function (event) {
var x = $(event.target).attr('class').split(' ')[0];
console.log(x)
$("#"+x).css("height", "0px");
$("#pnlSubmenu").slideToggle('fast');
});
like I said, this is a draft to get you going, this is not a perfect solution.
I hope this helps

Simple Jquery can do the trick
function showE() {
$("#h1").removeClass("hide");
}
function don() {
if ($('#h1').is(':hover')) {
$("#h1").removeClass("hide");
} else {
$("#h1").addClass("hide");
}
}
.hide {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id="h" href="#" onmouseover="showE()" onmouseout="don()">show</a>
<div id="c" onmouseover="showE();" onmouseout="javascript:$('#h1').addClass('hide');">
<div id="h1" style="float: inherit; overflow: hidden; position: absolute;height:15px;
background-color: Gray; border-radius: 0 5px 5px 5px;" class="hide">
Enter your email if you have posted this ad
</div>
</div>

Check this new one link https://jsfiddle.net/rxLg0bo4/9/
$('.menu_link').hover(function () {
$("#pnlSubmenu").slideDown('slow');
});
$('#pnlSubmenu').on("mouseenter", function () {
$(this).show();
});
$('#pnlSubmenu').mouseleave(function () {
$(this).hide();
});
$('.menu_link').mouseleave(function () {
$("#pnlSubmenu").hide();
});

Related

How Can I show two tooltips on the same div with Materializecss?

Hello Guys I have a form en Materialize and I use collapsible component to display it. For each category, I need to display in the accordion (the header of each seccion) two tooltips, the first to show the name of the category and the second to show the text "Active" of an option selected from the form.
I have the next code:
function obtain() {
var selectCloud_and_or = $("#And_OrCloud").find("option:selected").text();
$("#cloud_AndOr").text(selectCloud_and_or);
}
$('select').change(obtain);
obtain();
.collapsible {
margin-left: 80px;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.0/css/materialize.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.0/js/materialize.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('.collapsible').collapsible();
});
</script>
<!-- select option -->
<script type="text/javascript">
$(document).ready(function() {
$('select').material_select();
});
$('select').material_select('destroy');
</script>
<!-- Tooltip -->
<script type="text/javascript">
$(document).ready(function() {
$('.tooltipped').tooltip({
delay: 50
});
});
</script>
<ul class="collapsible" data-collapsible="accordion">
<li>
<a class=" tooltipped" data-position="left" data-delay="50" data-tooltip="First">
<div class="collapsible-header"><i class="wi wi-cloudy"></i></i><span id="cloud_AndOr"></span><span class="clouds"></span></a>
</div>
<div class="collapsible-body">
<div class="row">
<div class="col s12 offset-s4">
<select class="and-or" id="And_OrCloud" name="a_o[]">
<option value="4" selected="selected">And</option>
<option value="5">Or</option>
</select>
</div>
</div>
</div>
</li>
I just want to have two tooltip element, one appears when the user put the mouse throught accordion(the header), in any part of the accordion, and the second needs to appears when the user put the mouse on the option "and/or" that is placed in the accordion (the header). Any idea of how to do that? Right now I just have only one tooltip, but I need two. How can I add the second?
You need to just add the class "tooltipped" and data-tooltip="Message" to your collapsible-header.
What it does?
It basically treats your collapsible-header as a another container which needs to be tooltiped and you can add the desired message in the data-tooltip.
function obtain() {
var selectCloud_and_or = $("#And_OrCloud").find("option:selected").text();
$("#cloud_AndOr").text(selectCloud_and_or);
}
$('select').change(obtain);
obtain();
.collapsible {
margin-left: 80px;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.0/css/materialize.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.0/js/materialize.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('.collapsible').collapsible();
});
</script>
<!-- select option -->
<script type="text/javascript">
$(document).ready(function() {
$('select').material_select();
});
$('select').material_select('destroy');
</script>
<!-- Tooltip -->
<script type="text/javascript">
$(document).ready(function() {
$('.tooltipped').tooltip({
delay: 50
});
});
</script>
<ul class="collapsible" data-collapsible="accordion" >
<li>
<a class=" tooltipped" data-position="left" data-delay="50" data-tooltip="First">
<div class="collapsible-header tooltipped" data-tooltip="Another tooltip"><i class="wi wi-cloudy"></i></i><span id="cloud_AndOr"></span><span class="clouds"></span></a>
</div>
<div class="collapsible-body">
<div class="row">
<div class="col s12 offset-s4">
<select class="and-or" id="And_OrCloud" name="a_o[]">
<option value="4" selected="selected">And</option>
<option value="5">Or</option>
</select>
</div>
</div>
</div>
</li>

How to show a specific div when a link is clicked and hide others?

I want to show only one div when the user clicks the links on the icon bar and hide others. When the user clicks home link of the icon bar only 'hoediv'is visible and others hidden.
My work is below please help!!
<!doctype HTML>
<head>
<div class="main-header-div">
<a id="home" href="" > Home</a>
<a id="about" href=""> About us</a>
</div>
</head>
<body>
<script>
$(function(){
$("#home").click(function(){
$("#homediv").show();
$("#aboutus").hide();
return false;
});
});
</script>
<div id="homediv" style="color:white; background-color:red;height:89px;
width:100%;font-size:150%; display:none;">This is my site.
</div>
<div id="aboutus" style="display:none;">
this is about us page
</div>
</body>
</html>
What you need to fix?
Firstly, <head></head> only includes metadata, the rest should be in the <body></body>.
If you're not going to make use <a> anchor tags for hyperlinking, then pass the value href="JavaScript:Void(0);" (The void operator evaluates the given expression and then returns undefined) or better yet, don't use anchor tags better yet span or button.
You didn't import jquery.js in your html file.
You can make this a lot more effecient, but I'd suggest you learn some basic html from the widely available sources and then CSS, Jquery,etc.
Sources to refer:
https://www.w3schools.com/html/html5_intro.asp
https://www.w3schools.com/css/default.asp
https://www.w3schools.com/jquery/default.asp
$(function() {
$("#home").click(function() {
$("#homediv").show();
$("#aboutus").hide();
});
$("#about").click(function() {
$("#homediv").hide();
$("#aboutus").show();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main-header-div">
<a id="home" href="JavaScript:Void(0);"> Home</a>
<a id="about" href="JavaScript:Void(0);"> About us</a>
</div>
<div id="homediv" style="color:white; background-color:red;height:89px;
width:100%;font-size:150%; display:none;">This is my site.
</div>
<div id="aboutus" style="display:none;">
this is about us page
</div>
If you want to simplify it, you can use classes and data attributes to toggle wanted content:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main-header-div">
<a class="header-link" id="home" data-toggle="homediv" href="#" > Home</a>
<a class="header-link" id="about" data-toggle="aboutus" href="#"> About us</a>
</div>
<div class="content" id="homediv" style="color:white; background-color:red;height:89px;
width:100%;font-size:150%; display:none;">
This is my site.
</div>
<div class="content" id="aboutus" style="display:none;">
this is about us page
</div>
<script>
$(document).ready(function() {
$(".header-link").click(function(){
$(".content").hide();
$("#" + $(this).attr("data-toggle")).show();
});
});
</script>

Displaying info from sidebar on the same page

I am a beginner at this stuff, and right now I am working on a project. To keep it simple, my project right now has a sidebar that has four different options. (Schedule, progress, add course, drop course). I want users to be able to click on this options (after expanding the side bar) and display the information from which ever of the four they clicked on. I want this information to display on the same page, not link to another page. I have done this before by having invisible pages and using a showpage function. This time around though it is coded differently with classes, and I'm not sure how to go about this. Any help is appreciated!
Note: I don't have any data for these 4 pages right now - I just want to set it up so they function right now. To keep it short: I'm asking what code I need and where to display information (Ex: "Here is your schedule") on the same page when Schedule is clicked.
<!doctype html>
<html>
<head>
<title>STUDENT SCHEDULER APP</title>
<link rel="stylesheet" type="text/css" href="ssaStyles.css">
<script src="jquery-3.2.1.min.js"></script>
<script>
$(function() {
console.log("jQuery was loaded");
});
$(document).ready(function() {
function toggleSidebar() {
$(".button").toggleClass("active");
$("main").toggleClass("move-to-left");
$(".sidebar-item").toggleClass("active");
}
$(".button").on("click tap", function() {
toggleSidebar();
});
$(document).keyup(function(e) {
if (e.keyCode === 27) {
toggleSidebar();
}
});
});
</script>
</head>
<body>
<div id="header">
<h1>Welcome!</h1>
</div>
<div class="nav-right visible-xs">
<div class="button" id="btn">
<div class="bar top"></div>
<div class="bar middle"></div>
<div class="bar bottom"></div>
</div>
</div>
<!-- nav-right -->
<main>
<nav>
<div class="nav-right hidden-xs">
<div class="button" id="btn">
<div class="bar top"></div>
<div class="bar middle"></div>
<div class="bar bottom"></div>
</div>
</div>
<!-- nav-right -->
</nav>
</main>
<div class="sidebar">
<ul class="sidebar-list">
<li class="sidebar-item">Schedule
</li>
<li class="sidebar-item">Progress
</li>
<li class="sidebar-item">Add a course
</li>
<li class="sidebar-item"><a href="#" class="sidebar-anchor">Drop a
course</a></li>
</ul>
</div>
</body>
</html>
Its really simple all you have to do is create sections and these sections will be linked to your link, let me show you how.
<html>
<head>
<title>Example</title>
</head>
<body>
<!--create the links but add a # sign before you give them the section names-->
<div class="side-nav">
About
Contact
</div>
<!--Create an id with the same name as the link href source but leave out the # sign-->
<!--this is the about section-->
<div id="about">
<h1>Hello</h1>
</div>
<!--this is the contact section-->
<div id="contact">
<p> done</p>
</div>
</body>
</html>
the name you give your link should be the same as the div id name, and you will have to disable overflow in CSS if you want to....hope it made sense, if you need more help just ask.

How to addClass & addRemove class or change property using .css?

I have tried many ways but still not working...
click the submit button & expand button(tab) to display the chatbot and hide the tab
click the minimize button(chatbot) to hide the chatbot and display the tab
1) addClass/RemoveClass
default.htm
<!-- ------- button ------- -->
<div class="ask-button-container">
<button id="submit" class="btnSubmit">Ask</button>
</div>
<!-- ------- tab ------- -->
<div id="tab{{__SELF__.id}}" name="{{__SELF__.id}}" class="my-tab">
<div id="tab-minimize">
<div id="tab-label"></div>
<div id="tab-expand{{__SELF__.id}}" class="tab-expand tab-btn"></div>
<div id="tab-close{{__SELF__.id}}" class="tab-close tab-btn"></div>
</div>
</div>
<!-- ------- chatbot ------- -->
<div id="chatbot-window-wrapper{{__SELF__.id}}" class="cb-window hidden" style="position: fixed;">
<div id="control-bar">
<div id="chatbot-minimize{{__SELF__.id}}" class="minus pull-right chatbot-btn"><span class="glyphicon glyphicon-minus-sign"></div>
</div>
<iframe id="chatbot-window" name="chatbot-window" scroll="" width="100%" height="100%" frameborder="2" src="{{ 'page1'|page }}"></iframe>
</div>
JS
$(function(){
$(".tab-close").click(function() {
$(this).closest(".my-tab").addClass("hidden"); //working
});
$(".tab-expand").click(function() {
var chatbot = $(this).closest(".cb-window");
chatbot.removeClass("hidden");
$(this).closest(".my-tab").addClass("hidden"); //working
});
$(".btnSubmit").click(function() {
var chatbot = $(this).closest(".cb-window");
chatbot.removeClass("hidden");
$(this).closest(".my-tab").addClass("hidden");
});
$(".minus").click(function() {
var chatbot = $(this).closest(".cb-window");
chatbot.addClass("hidden");
$(this).closest(".my-tab").removeClass("hidden");
});
});
2) .css
$(this).closest(".cb-window").css("visibility", "visible");
Can someone please show me how to do it? Thank you.
or any other ways using jquery? .show/.hide , .toggle?
The problem was with your way of reaching the proper element and your understanding on closest option of jquery.
As per definition - The closest() method returns the first ancestor of the selected element. An ancestor is a parent, grandparent,
great-grandparent, and so on.
When you say closest on btnSubmit click event, the .cb-window was never its ancestor so $(this).closest('.cb-window') would basically fail. You need to traverse back to btnSubmit's parent who will be sibling of .cb-window and then you can easily get that element. Below is how you can achieve it. Also, you do not need multiple $(function(){ which is equivalent to $(document).ready and only one is sufficient.
$(function() {
$(".btnSubmit").click(function() {
$(this).closest(".ask-button-container").siblings('.cb-window').removeClass("hidden");
//closest ancestor would be .ask-button-container and its sibling is .cb-window
$(this).closest(".my-tab").addClass("hidden");
//did not find .my-tab element in your `html`
});
$(".minus").click(function() {
$(this).closest('.cb-window').addClass("hidden");
$(this).closest(".my-tab").removeClass("hidden");
});
});
.hidden {
display: none;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<div class="ask-button-container">
<button id="submit" class="btnSubmit">Ask</button>
</div>
<div id="chatbot-window-wrapper{{__SELF__.id}}" class="cb-window hidden" style="position: fixed;">
<div id="control-bar">
<div id="chatbot-minimize{{__SELF__.id}}" class="minus pull-right chatbot-btn"><span class="glyphicon glyphicon-minus-sign"></div>
</div>
<iframe id="chatbot-window" name="chatbot-window" scroll="" width="100%" height="100%" frameborder="2" src="{{ 'page1'|page }}"></iframe>
</div>
When we click or do operation on an element we can add or remove class and you can try like this.
$('#elm').hover(
function(){ $(this).addClass('hover') },
function(){ $(this).removeClass('hover') }
)
the problem that you don't target the element you want in a right way .. you need to use
$(function(){
$(".tab-close").click(function() {
$(this).closest(".my-tab").addClass("hidden"); //working
});
$(".tab-expand").click(function() {
var chatbot = $(this).closest('.my-tab').next(".cb-window");
chatbot.removeClass("hidden");
$(this).closest(".my-tab").addClass("hidden"); //working
});
$(".btnSubmit").click(function() {
var chatbot = $(this).closest('div').next().next(".cb-window");
chatbot.removeClass("hidden");
$(this).closest('div').next(".my-tab").addClass("hidden");
});
$(".minus").click(function() {
var chatbot = $(this).closest(".cb-window");
chatbot.addClass("hidden");
chatbot.prev(".my-tab").removeClass("hidden");
});
});
.hidden{
display : none;
}
.minus{
padding : 5px;
background : red;
color: #fff;
text-align : center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- ------- button ------- -->
<div class="ask-button-container">
<button id="submit" class="btnSubmit">Ask</button>
</div>
<!-- ------- tab ------- -->
<div id="tab{{__SELF__.id}}" name="{{__SELF__.id}}" class="my-tab">
<div id="tab-minimize">
<div id="tab-label"></div>
<div id="tab-expand{{__SELF__.id}}" class="tab-expand tab-btn"></div>
<div id="tab-close{{__SELF__.id}}" class="tab-close tab-btn"></div>
</div>
</div>
<!-- ------- chatbot ------- -->
<div id="chatbot-window-wrapper{{__SELF__.id}}" class="cb-window hidden" style="position: fixed;">
<div id="control-bar">
<div id="chatbot-minimize{{__SELF__.id}}" class="minus pull-right chatbot-btn"><span class="glyphicon glyphicon-minus-sign">-</span></div>
</div>
<iframe id="chatbot-window" name="chatbot-window" scroll="" width="100%" height="100%" frameborder="2" src="{{ 'page1'|page }}"></iframe>
</div>

how do you change img src with javascript depending on browser width?

I would like to change the source of the image element with the id of "front". I have a basic script at the top, but it doesn't work. I was just attempting to come up with something but I'm very bad at JavaScript.
<html>
<head>
<link rel="stylesheet" type="text/css" href="home.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="home.js"></script>
<script type="text/javascript">
var reswidth=screen.width;
if (reswidth<400){
var x = document.getElementsById("front");
x.src="../images/colbysmall.png"
}
</script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<div class="header">
<div class="navigation">
<ul>
<li><a id="home" href="">Home</a></li>
<li>Services</li>
<li>Portfolio</li>
<li>Contact</li>
<li></li>
</ul>
</div>
<div class="yellow"></div>
<div class="x"></div>
</div>
</head>
<body>
<div class="website">
<div class="logo"></div>
<img src="../images/ColbyFaceblack.jpg" id="front" width="100%" >
<div class="content">
<div class="menu"></div>
<div class="office"></div>
</div>
<div class="body2">
<img src="../images/simple_dashed_full.png" width="100%">
<div class="weboutline">
<img src="../images/web_outline.png" width="100%">
</div>
<div class="body2img2">
<img src="../images/portfolio.png" width="100%">
</div>
<div class="body2img3">
<img src="../images/blog.png" width="100%">
</div>
</div>
<div class="body3">
<img src="../images/body.png" width="100%">
<img class="touch" src="../images/get_in_touch_beige.png" width="50%">
</div>
<footer>
<img src="../images/footer.png" width="100%">
<div class="copyright"> copyright COLBY MOFFETT 2015 </div>
<div class="facebook"></div>
<div class="instagram"></div>
<div class="twitter"></div>
</footer>
</body>
</div>
</html>
This is right, but has to be executed on change of screen size:
$(function () {
$(window).resize(function () {
var reswidth=screen.width;
if (reswidth<400){
var x = document.getElementsById("front");
x.src="../images/colbysmall.png"
}
});
});
Also make sure you execute it inside the $(function () { }); to execute on the DOM loaded contents. So, to make sure it also executes when it gets loaded, you need to store it in a named function and execute it every time when the window is resized.
Your final code will be:
$(function () {
var reszWindow = function () {
var reswidth=screen.width;
if (reswidth<400){
var x = document.getElementsById("front");
x.src="../images/colbysmall.png"
}
};
reszWindow();
$(window).resize(reszWindow);
});
Since you are already using jQuery, the following could be one of the solutions:
$( window ).resize(function() {
if($( window ).width() < 400) {
$("img#front").attr("src","../images/colbysmall.png");
} else {
$("img#front").attr("src","../images/ColbyFaceblack.jpg");
}
});
(PS. Untested code) :)
changing image according to screen resolution:
$(window).resize(function() {
var scrWidth = $(window).width();
if(scrWidth < 500){
$('#myImage').attr('src', 'http://wallpaper-download.net/wallpapers/logo-wallpapers-google-chrome-background-wallpaper-33143.jpg');
}
if(scrWidth > 500){
$('#myImage').attr('src', 'http://www.bhmpics.com/download/google_colorful_background-1920x1080.jpg');
}
});
(random images from google)
Fiddle here: http://jsfiddle.net/59ehtLde/
Javascript aside, you can do this with just HTML5. Browser support isn't great, but isn't bad, either.
<picture>
<source media="(min-width: 400px)" srcset="../images/colbysmall.png">
<img src="../images/ColbyFaceblack.jpg">
</picture>
<p>With placeholders:</p>
<picture>
<source media="(min-width: 400px)" srcset="http://placehold.it/400?text=colbyfaceblack">
<img src="http://placehold.it/200?text=colbysmall">
</picture>

Categories