How can i enter an image url and get an image preview - javascript

$('[name="thumbnail"]').on('change', function() {
$('img.preview').prop('src', this.value);
});
<input name="thumbnail" placeholder="Post image URL here" />
<br />
<img src="http://webspace.webring.com/people/jv/vladilyich/preview.gif" class="preview" />
i'm looking for something to work like the js fiddle example.
but only i want it to work if a user were to enter img5.jpg for example, an image saved on desktop in the same directory as the index.html folder

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Image preview</title>
<link rel="stylesheet" href="">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#imgName').bind('input', function() {
$('#imageHolder').attr('src', $(this).val()); //concatinate path if required
});
});
</script>
</head>
<body>
<input type="text" name="" id="imgName" value="" placeholder="Input image name"><br><br>
<img name="imageHolder" id="imageHolder" src="http://i.imgur.com/zAyt4lX.jpg">
</body>
</html>

Related

How to change background color of the web page according to user choice?

I tried using the input color tag but i have to click twice on the tag to change the color any way to fix this
<!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" />
<title>Document</title>
</head>
<body>
<form action="">
<label for="color"> Color:</label>
<input
type="color"
name="color"
id="color"
value="#00FFFF"
onclick="changecolor()"
/><br />
</form>
<script>
function changecolor() {
let color = document.getElementById("color").value;
document.body.style.backgroundColor = color;
}
</script>
</body>
</html>
Your code does work, only you are using an incorrect event listener for what you are trying to do.
Simply change your event from onclick to onchange
Like this
<input
type="color"
name="color"
id="color"
value="#00FFFF"
onchange="changecolor()"
/>
Hopefully this makes sense.

How to call a function on form submit using external js file

I want to call a function when the user submit a form, and how to achieve this with external JS script.
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>teste</title>
<link rel="stylesheet" type="text/css" href="style.css">
<link type="text/javascript" src="main.js">
</head>
<body>
<h1 class="title">teste</h1>
<form action="a()">
<input type="input1" class="input1" name="verb" id="verb" placeholder="Verb"/>
</form>
</body>
</html>
main.js
function a(){
alert("verb");
}
Follow This code...
HTML file
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>teste</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1 class="title">teste</h1>
<form >
<input type="text" name="text" id="input" placeholder="Verb" />
<input type="submit" onclick="a()">
</form>
<script src="./index.js"></script>
</body>
</html>
JS File
function a() {
alert('hello');
}
You could add event listener on onsubmit event of form and you can add the external scripts by using script tag.
Example: how to add external JS file.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>teste</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="./main.js"></script>
</head>
<body>
//.. html
</body>
</html>
Working Example:
let formElem = document.getElementById("form");
formElem.addEventListener("submit", formSubmitHandler);
function formSubmitHandler(event) {
event.preventDefault();
console.log("Form submitted");
console.log("User entered: " + event.target.elements[0].value);
}
<h1 class="title">teste</h1>
<form id="form">
<input
type="input1"
class="input1"
name="verb"
id="verb"
placeholder="Verb"
/>
<button type="submit" >Submit</button>
</form>
You can add the formSubmitHandler in the main.js file.

Using map function of jquery to alert the values of checkbox

HTML Code:
<!DOCTYPE html>
<html lang="en">
<head>
<title>JobSeeker Registration Page </title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script type="text/javascript" src="../jquery-1.11.3.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script type="text/javascript" src="try.js"></script>
</head>
<body>
<div>
<form>
<input type="checkbox" name="cb" value="First" /> First
<input type="submit" value="Submit" id="sm" /> Submit
</form>
</div>
</body>
</html>
Jquery Code:
$(document).ready(function(){
//alert("hello");
$("#sm").click(function(event){
event.preventDefault();
var searchIDs =
$(".cb:checked").map(function(){
return $(this).attr('value');;
}).get();
alert(searchIDs.join(','));
});
});
I am trying to alert the values of the checkbox on to the browser. But no data is coming. Could anybody please tell me what is the problem with the above code.
You haven't added a class called .cb to your inputs so your selector is never picking them up.
Try this:
$("input[name=cb]:checked")
Or add the missing class:
<input type="checkbox" name="cb" class="cb" value="First" /> First

How to get piece of information from url given

Hi i have this html code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link type="text/css" rel="stylesheet" href="/style.css"/>
<link rel="icon" href="">
<title>Submit</title>
</head>
<body>
<script>
function show_alert() {
alert("Animes score is: ");
}
</script>
<div class=střed>
<form>
<div id="one"><input style="width: 500px" type="text" name="url" value="Enter myanimelist URL" onfocus="if (this.value=='Enter myanimelist URL') this.value='';"/></div>
<div id="two"><input type="image" onclick="show_alert();" name="submit" src="/Button1.png" border="0" alt="Submit" />
</div>
</form>
</div>
</body>
</html>
Now i want to get from user's url (for example: https://myanimelist.net/anime/5114/Fullmetal_Alchemist__Brotherhood) the anime's score from page's html (this part: <span itemprop="ratingValue">9.26</span> - so in this case it would be 9.26) and show it in alert box (doesn't need to be alert box thought, just somehow show it to user). Thanks for any help.

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.

Categories