Cant get click event to change h1 property - javascript

<!DOCTYPE html>
<html>
<head>
<title>JavaScript and the DOM</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<h1 id="myHeading">JavaScript and the DOM</h1>
<p>Making a web page interactive</p>
<script src="app.js"></script>
</body>
</html>
This is my html code and the js I have written is here
const myHeading = document.getElementById('myHeading');
myHeading.addEventListener('click', () => {
myHeading.style.color='blue';
});
It is very simple however for some reason the h1 tag does not seem to be turning blue on click event?

Maybe your dom is not ready when js executed. Try this
document.addEventListener("DOMContentLoaded", function() {
// your code
})

Related

Why script with Tensorflow.js src doesn't work?

If I remove src="https://cdn.jsdelivr.net/npm/#tensorflow/tfjs/dist/tf.min.js" then it shows my alert. What's wrong?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> Brain Tumor </title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="https://cdn.jsdelivr.net/npm/#tensorflow/tfjs/dist/tf.min.js">
alert("alert"); // doesn't work if there's src
/*async function LoadModels(){
model = undefined;
model = await tf.loadLayersModel("D:/user/diploma/models/models/model.json");
const image = tf.fromPixels("D:/user/diploma/IM-0115-0001.jpeg");
const prediction = model.predict(image);
alert(prediction);
}
LoadModels();*/
</script>
</head>
...
</html>
EDIT:
You have to add alert in another script tag.
<script src="https://cdn.jsdelivr.net/npm/#tensorflow/tfjs/dist/tf.min.js"></script>
<script>
alert("alert");
</script>

Using JavaScript to change local src of an HTML iframe

Lets say you have a main .html:
<!DOCTYPE html>
<html>
<head>
<title>Page One</title>
<script type="text/javascript" src="javaS.js"></script>
<link rel="stylesheet" type="text/css" href="CSS.css">
</head>
<body id="main">
<h3>Test switching html content inside iframe</h3>
<p>iframe:</p>
<iframe src="" id="iframe1"></iframe>
</body>
</html>
And a secondary .html:
<!DOCTYPE html>
<html>
<head>
<title>Page two</title>
<link rel="stylesheet" type="text/css" href="CSS.css">
</head>
<body id="test">
<h3>Test subject</h3>
<p>subjugate text</p>
</body>
</html>
How would you display the local second .html inside the iframe element of the first .html, using only JavaScript?
I have tried using these snippets:
window.onload = function() {window.frames['iframe1'].location.replace("Secondary.html");}
.
var iframe = document.getElementById('iframe1');
iframe.src = "second.html";
...But these haven't worked. I'm new at this so the answer might be fairly obvious, but any guidance would be very much appreciated!
I use this and it works well:
window.onload = function()
{
document.getElementById('iframe1').src = "Secondary.html";
}
document.getElementsByTagName("iframe")[0].setAttribute("src", "http://your-url.com");
Your second snippet is perfect. You just have to make sure that it runs when iframe DOM element exists - in window.onload.
I just combined the two exampples you had tried to make one working example, see here: https://jsfiddle.net/4p18mxg9/9/
var iframe = document.getElementById('iframe1');
window.onload = function() {
iframe.src = "second.html";
}

How to integrate Twitter Bootstrap into Backbone app

So I have the following index.html:
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Project</title>
 <link href="public/css/bootstrap.css" rel="stylesheet">
</head>
<body>
<script src="lib/jquery-1.11.3.js"></script>
<script src="lib/underscore.js"></script>
<script src="lib/backbone.js"></script>
<script src="lib/bootstrap.js"></script>
<script src="public/js/main.js"></script>
</body>
main.js
(function($){
// Object declarations goes here
var FormLoanView = Backbone.View.extend({
tagName: 'div',
template: _.template('<p class="text-uppercase">Uppercased text.</p>'),
initialize: function(){
var data = this.$el.html(this.template())
$('body').html(data)
}
})
$(document).ready(function () {
var LoanView = new FormLoanView({
});
});
})(jQuery);
I am trying to create <p class="text-uppercase">Uppercased text.</p> and append it to the body in my index.html. It does return the p element, but the bootstrap styles don't kick in because "Uppercased text." does not return with uppercase letters. Anyone know why this is happening? Can bootstrap classes not be using in _.template?
It looks like your bootstrap.css isn't included properly. Try using the CDN or fixing the relative path of the css file.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Project</title>
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<script src="lib/jquery-1.11.3.js"></script>
<script src="lib/underscore.js"></script>
<script src="lib/backbone.js"></script>
<script src="public/js/main.js"></script>
</body>
Here's an example using only the CSS: https://jsfiddle.net/9nm5f0x9/
You don't need to include the bootstrap JS file as another commenter stated.

(Object object) has no method effect

I can't seem to understand what is wrong with the code.
I've added jquery and jquery UI still have the :
[objec object] has no method effect error.
Here is the html code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<link rel="stylesheet" href="stylesheet.css" type="text/css"/>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.2.min.js">
</script>
<script type="text/javascript" src = 'script.js'></script>
</head>
<body>
<div id = 'container'>
<div id ='one' class = 'inside'>1</div>
<div id ='two' class = 'inside'>2</div>
<div id ='three' class = 'inside'>3</div>
<div id ='four'class = 'inside'>4</div>
<div id ='five' class = 'inside'>5</div>
</div>
</body>
</html>
And the javascript code :
$(document).ready( function () {
alert('JQ on');
$('.inside').mouseover(function () {
$(this).fadeTo(1000,1);
});
$('.inside').mouseleave(function () {
$(this).fadeTo(500,0.2);
})
$('.inside').click(function () {
$(this).effect('explode');
})
})
Any ideas?
You've included the jQuery UI stylesheet, but not the JavaScript for it.
You need to add:
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
(Or another URI that supplies it) after you include the jQuery JS.
I suppose you need to include jQuery UI

Perform JQuery actions on ajax loaded page

I am trying to access content inside a html file that I loaded into a div using jquery.load.
My index page looks like this:
<!DOCTYPE html>
<html>
<head>
<title>
</title>
<script src="jquery.js" type="text/javascript"></script>
<script src="script.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="content">
</div>
</body>
</html>
My script so far looks like this:
$(document).ready(function() {
$("#content").load("content.html");
$("#content").click(function(){
alert($(this).attr("id"));
});
});
The content.html page:
<!DOCTYPE html>
<html >
<head>
<title></title>
</head>
<body>
<h1 id="header1">
Content
</h1>
<p>
This is a paragraph
</p>
<p>
This is another paragraph
</p>
</body>
</html>
So what I want to happen is:
When I click on the tag in the content div it should display that tag's id - namely "header1", but currently its just displaying "content". How can I achieve this?
Thank you in advance
Bind the event handler to every element in content.
$(document).ready(function() {
$("#content").load("content.html");
$("#content, #content *").click(function(e){
alert(this.id);
e.stopPropagation();
});
});
Or let the events propogate:
$(document).ready(function() {
$("#content").load("content.html");
$("#content").click(function(e){
alert(e.target.id); //may return undefined if no id is assigned.
});
});
Working Example: http://jsfiddle.net/5JmsP/

Categories

Resources