My code of javascript Show and hide does not work? - javascript

I am doing a task of shwoing and hiding container1 of confused why my code does not work
My task is :
to click on button of show (will display container1)
And button of hide (Hiding the container1)
this is the html
function Show() {
document.getElementById('Khenchela').style.display = 'block'
}
function Hide() {
document.getElementById('Khenchela').style.display = 'none'
}
<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="3.css">
<head>
<title>JAVA SCRIPT WEBSITE</title>
</head>
<body>
<button oneclick="Show()">Hide London</button>
<button oneclick="Hide()">Show London</button>
<div id="Khenchela" class="container 1">
<h3>Khenchela</h3>
<p>Khenchela is the Tadjalt city of Algeria </p>
</div>
<div id="Batna" class="container 2">
<h3>Batna </h3>
<p>Batna is the Tnakt city of algeria</p>
</div>
<div id="Biskra" class="container 3">
<h3>Biskra</h3>
<p>Biskra is the 'after' Tnakt city of algeria</p>
</div>
<script src="2.js" type="text/javascript"></script>
</body>
</html>

You have typo it should be onclick not oneclick.
function Show() {
document.getElementById('Khenchela').style.display = 'block'
}
function Hide() {
document.getElementById('Khenchela').style.display = 'none'
}
<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="3.css">
<head>
<title>JAVA SCRIPT WEBSITE</title>
</head>
<body>
<button onclick="Hide()">Hide London</button>
<button onclick="Show()">Show London</button>
<div id="Khenchela" class="container 1">
<h3>Khenchela</h3>
<p>Khenchela is the Tadjalt city of Algeria </p>
</div>
<div id="Batna" class="container 2">
<h3>Batna </h3>
<p>Batna is the Tnakt city of algeria</p>
</div>
<div id="Biskra" class="container 3">
<h3>Biskra</h3>
<p>Biskra is the 'after' Tnakt city of algeria</p>
</div>
<script src="2.js" type="text/javascript"></script>
</body>
</html>

Related

Why am i getting undefined in the console log instead of the input value?

I am trying to get the input value as my result but after clicking on the button I am getting 'undefined' as the console result. Please help to understand the reason. Sharing the HTML and js code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="movie-search-engine.css">
<script src="scripts/movie-search-engine.js" defer></script>
<title>Movie Search Engine</title>
</head>
<body>
<div class="searchBox">
<input type="text" class="searchBox" placeholder="Movie Name">
<button onclick="moviesearchEngine()" class="searchBoxID" >Search</button>
</div>
<div class="movieDetails">
<h2 class="movieTitle">Movie Title</h2>
<div class="movieData">
<p class="yearofRelease">Year of Release</p>
<p class="movieTitle">Movie Title</p>
<p class="genre">Genre</p>
<p class="director">Director</p>
<p class="plot">Plot</p>
</div>
</body>
</html>
const moviesearchEngine=()=>{
let searchBox = document.querySelector('.searchBox');
console.log(searchBox.value)
}
You are using a class for multiple elements which is why undefined is shown. Among many ways, I will discuss two here, you have to either use indices with classes if using queryselector to get the correct element or use an ID to select the input element. Here I have used ID to get the input searchbox. See the following code:
const moviesearchEngine = () => {
let searchBox = document.querySelector('#searchBoxInput');
console.log(searchBox.value)
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="movie-search-engine.css">
<script src="scripts/movie-search-engine.js" defer></script>
<title>Movie Search Engine</title>
</head>
<body>
<div class="searchBox">
<input type="text" id="searchBoxInput" placeholder="Movie Name">
<button onclick="moviesearchEngine()" class="searchBoxID">Search</button>
</div>
<div class="movieDetails">
<h2 class="movieTitle">Movie Title</h2>
<div class="movieData">
<p class="yearofRelease">Year of Release</p>
<p class="movieTitle">Movie Title</p>
<p class="genre">Genre</p>
<p class="director">Director</p>
<p class="plot">Plot</p>
</div>
</body>
</html>
Your div <div class="searchBox"> also contain a class "searchBox" so it selected that.
Fixed: Add an input selector after class selector.
const moviesearchEngine=()=>{
let searchBox = document.querySelector('.searchBox input');
console.log(searchBox.value)
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="movie-search-engine.css">
<script src="scripts/movie-search-engine.js" defer></script>
<title>Movie Search Engine</title>
</head>
<body>
<div class="searchBox">
<input type="text" class="searchBox" placeholder="Movie Name">
<button onclick="moviesearchEngine()" class="searchBoxID" >Search</button>
</div>
<div class="movieDetails">
<h2 class="movieTitle">Movie Title</h2>
<div class="movieData">
<p class="yearofRelease">Year of Release</p>
<p class="movieTitle">Movie Title</p>
<p class="genre">Genre</p>
<p class="director">Director</p>
<p class="plot">Plot</p>
</div>
</body>
</html>

Adding li element with Javascript and Jquery

I just start to use JQuery and I want to add li element in my HTML when I click on a button.
My Html code :
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/style.css">
<script type="text/javascript" src="js/index.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<div class="filtres">
<input id="filtre1" type="button" value="filtre 1" onclick="test()">
</div>
<div id="liste">
<ul>
<li>Bonjour</li>
</ul>
</div>
</body>
</html>
And my js file :
function test() {
console.log("On clique");
$("<li>Coucou</li>").appendTo("<ul>");
}
Unfortunately, It doesn't work and I don't understand why.
Do you have any idea ?
The problem is because your selector is incorrect. appendTo('<ul>') is trying to create a new ul element in memory and append the li to that. You instead need to use appendTo('ul'), without the angle brackets.
I'd also suggest you use unobtrusive event handlers instead of the outdated onclick attributes. Here's the full example:
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/style.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</head>
<body>
<div class="filtres">
<input id="filtre1" type="button" value="filtre 1" />
</div>
<div id="liste">
<ul>
<li>Bonjour</li>
</ul>
</div>
</body>
</html>
jQuery(function($) {
$('#filtre1').on('click', function() {
console.log("On clique");
$("<li>Coucou</li>").appendTo("ul");
});
})
Try this:
function test() {
$('ul').append('<li>Hello!</li>');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="liste">
<ul>
<li>Bonjour</li>
</ul>
</div>
<button onclick="test()">append</button>
append() will add an element at the end
function test() {
$('#liste > ul').append('<li>Coucou</li>');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="filtres">
<input id="filtre1" type="button" value="filtre 1" onclick="test()">
</div>
<div id="liste">
<ul>
<li>Bonjour</li>
</ul>
</div>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/style.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</head>
<body>
<div class="filtres">
<input id="filtre1" type="button" value="filtre 1" />
</div>
<div id="liste">
<ul>
<li>Bonjour</li>
</ul>
</div>
</body>
</html>

HTML bootstrap collapse doesnt working at first button press properly

Can you help me with that bootstrap and javascript collapse please?
I have two collapse cards. The first should card be visible than you click on the link. After the click should show next card and the first should disappear.
It is functional but at the first click there are both cards. Thats fix after the second click. Thank u for your help.
HTML:
<div class="card text-center">
<div class="card-header ">
<a class="nav-link" id="spust" href="#">Kontaktní formulář</a>
</div>
<div id="kontakty">
</div>
<div class="collapse" id="konform">
</div>
</div>
Javascript:
var collapsestate = false;
jQuery('#spust').on("click",function(e) {
if (collapsestate == false){
$('#kontakty').collapse('hide');
$('#konform').collapse('show');
collapsestate = true;
}
else{
$('#konform').collapse('hide');
$('#kontakty').collapse('show');
collapsestate = false;
}
console.log(collapsestate);
});
try with this snippet code... I think this snippet is as per your requirements.
$(document).ready(function(){
var collapsestate = false;
$('#kontakty').collapse('show');
$('#spust').on("click",function(e) {
if (collapsestate == false){
$('#kontakty').collapse('hide');
$('#konform').collapse('show');
collapsestate = true;
}
else{
$('#kontakty').collapse('show');
$('#konform').collapse('hide');
collapsestate = false;
}
console.log(collapsestate);
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Demo</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="card text-center">
<div class="card-header ">
<a class="nav-link" id="spust" href="#">Kontaktní formulář</a>
</div>
<div id="kontakty">
abcd
</div>
<div class="collapse" id="konform">
1234
</div>
</div>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>
You can add "display:none" to make 'kontakt' invisible before click it:
<div id="kontakty" style="display:none">
</div>

Thymeleaf + HTML5 - why can't I access my javascripts?

I have an Spring MCV 4 + HTML5 + Thymeleaf based application I can't seem to use javascript on pages.
The webapp structure is:
webapp
assets
css
img
js
i18n
messages.properties
WEB-INF
html
fragments
common.html
default.html
footer.html
header.html
login.html // exclusively for ../login.html
sidebar.html
home
welcomeSignedIn.html
client
home.html
create.html
In my login page, since it is a single page, with only a picture in the background, I have a piece of JS that works flawlessly:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="fragments/login :: loginFragment">
<meta charset="UTF-8" />
<title><th:text="#{app.name}"></label></th:text>
</head>
<body>
<div class="login-container animated fadeInDown">
<div class="loginbox bg-white">
<form th:action="#{/login}" method="post">
<div class="loginbox-title">SIGN IN</div>
<div class="loginbox-textbox">
<input type="text" id="ssoId" name="ssoId" class="form-control" placeholder="Username" />
</div>
<div class="loginbox-textbox">
<input type="password" id="password" name="password" class="form-control" placeholder="Password" />
</div>
<div class="loginbox-forgot">
Forgot Password?
</div>
<div class="loginbox-submit">
<input type="submit" class="btn btn-primary btn-block" value="Login"/>
</div>
</form>
</div>
</div>
<script type="text/javascript">
$(function () {
$('#ssoId').focus();
});
</script>
</body>
</html>
Login page has its own fragment, because it's a single, no menus, no sidebar page. All other are.
For may default templating, I have a file called fragments/default.html reponsible for pages organization.
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head th:include="fragments/common :: commonFragment">
<meta charset="UTF-8" />
<title th:text="#{app.name}"> </title>
</head>
<body>
<div th:id="defaultFragment" th:fragment="defaultFragment">
<div th:replace="fragments/header :: headerFragment"></div>
<div class="main-container container-fluid">
<div class="page-container">
<div th:replace="fragments/sidebar :: sidebarFragment"></div>
<div class="page-content">
<div class="page-body">
<div layout:fragment="content"></div>
</div>
</div>
</div>
</div>
<div th:replace="fragments/footer :: footerFragment"></div>
</div>
</body>
</html>
So that, for every page, I get included the following links and scripts from fragments/common.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:id="commonFragment" th:fragment="commonFragment">
<link rel="icon" th:href="#{/assets/img/favicon.png}" type="image/x-icon" />
<link rel="stylesheet" th:href="#{/assets/css/bootstrap.min.css}" />
<link rel="stylesheet" th:href="#{/assets/css/font-awesome.min.css}"/>
<link rel="stylesheet" th:href="#{/assets/css/beyond.min.css}"/>
<link rel="stylesheet" th:href="#{/assets/css/demo.min.css}" />
<link rel="stylesheet" th:href="#{/assets/css/animate.min.css}" />
<link rel="stylesheet" th:href="#{/assets/css/skins/darkred.min.css}" />
<link rel="stylesheet" th:href="#{http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,400,600,700,300}" />
<!--Skin Script: Place this script in head to load scripts for skins and rtl support-->
<script type="text/javascript" src="/assets/js/skins.min.js" th:src="#{/assets/js/skins.min.js}"></script>
<script type="text/javascript" src="/assets/js/jquery-2.0.3.min.js" th:src="#{/assets/js/jquery-2.0.3.min.js}"></script>
<script type="text/javascript" src="/assets/js/bootstrap.min.js" th:src="#{/assets/js/bootstrap.min.js}"></script>
<script type="text/javascript" src="/assets/js/slimscroll/jquery.slimscroll.min.js" th:src="#{/assets/js/slimscroll/jquery.slimscroll.min.js}"></script>
<script type="text/javascript" src="/assets/js/beyond.js" th:src="#{/assets/js/beyond.js}"></script>
<script type="text/javascript" src="/assets/js/beyond.min.js" th:src="#{/assets/js/beyond.min.js}"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<div class="container" />
</body>
</html>
My fragments/header.html is defined as:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="fragments/common :: commonFragment">
</head>
<body>
<div th:id="headerFragment" th:fragment="headerFragment">
// content
</div>
</body>
</html>
fragments/sidebar.html is very similar to `fragments/header.html, just different classes and content.
So, on every page I have on my application, I do something like this:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorator="fragments/default" xmlns:s="http://www.springframework.org/tags">
<head >
<title th:text="#{view.index.title}">Welcome!</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div layout:fragment="content">
// content
</div>
</body
</html>
I'm sure it's something quite stupid, but I'm failing to see why can't I get javascript working on my pages, even for something quite as simple as:
<script type="javascript" >
$(function () {
alert('hello');
});
</script>
And, what's bothering (a lot!) is that if I on the browser something like:
localhost:8080/appName/assets/js/bootstrap.min.js
It will show the contents of the file.
What am I missing here?
Thanks for the input, guys! I got it working. What I did is placing the JavaScript section inside the <div layout:fragment="content">*HERE*</script> area, like on the example bellow.
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorator="fragments/default" xmlns:s="http://www.springframework.org/tags">
<head >
<title th:text="#{view.index.title}">Welcome!</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div layout:fragment="content">
<script type="text/javascript" >
*JavaScript area*
</script>
// content
</div>
</body
</html>
Hope this will save someone's precious time!
I think you have problems because there are 2 jquery files included to your fragments/common.html (check it via Firebug or something like that):
<head th:id="commonFragment" th:fragment="commonFragment">
...
<script type="text/javascript" src="/assets/js/jquery-2.0.3.min.js" th:src="#{/assets/js/jquery-2.0.3.min.js}"></script>
...
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
</head>
Try to remove one of the definitions.

CSS and JS are not working on live server while I transfer to there

I created a coming soon page for my new portfolio domain, on a local server. When I'm trying to transfer it to my hosting panel on public html, just HTML file are showing but CSS and JS files are not getting included, and they are not working properly.
What do I need to change there for using on live server ?
I uploaded all folders, img, js, css and files.
Here is a link for live preview (Dropbox): Coming_soon_template
Here is my code for local server:
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>This site is in under construction</title>
<meta name="description" content="This site for now is in under construction will be live after some days.For more you can knock me on there lover.mahabub#gmail.com">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/normalize.css">
<link href='http://fonts.googleapis.com /css?family=Lato:400,300,100,300italic' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="include/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="css/TimeCircles.css">
<link rel="stylesheet" href="css/main.css">
<script src="js/vendor/modernizr-2.8.3.min.js"></script>
</head>
<body>
<div id="coming-soon-total">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="header-text">
<span>A creation of Mahbub Rahman**</span>
<h1>Wait for difference!</h1>
<p>Long way to go just keep patience.</p>
</div>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="example" data-date="2015-12-23 00:00:00"> </div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="social-area">
<p> Get in touch</p>
<ul class="social-list">
<li>Facebook</li>
<li>Twitter</li>
<li>Skype</li>
<li>whatsapp</li>
<li>gplus</li>
</ul>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/TimeCircles.js"></script>
<script type="text/javascript">
$('document').ready(function() {
'use strict';
$(".example").TimeCircles({
start: true,
time: {
Days: {color: "#4A616E"},
Hours: {color: "#5E524A"},
Minutes: {color: "#2E2F2A"},
Seconds: {color: "#9BA7B5"}
},
circle_bg_color: "#7F8382",
bg_width: .8,
});
});
</script>
</body>
</html>

Categories