...

View Full Version : How to dynamically change content



OM2
07-12-2012, 12:59 AM
*Complete* newbie question: how do I change content dynamically in my HTML using Javascript?

Well... I thought I had the answer:



<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="utf-8">
<title>My Development Website</title>
<script type="text/javascript">

$("#my-name").html("Text is here");

</script>

</head>
<body>
<div id="my-name"></div>
</body>
</html>


Surely, my DIV should change content?

Where am I going wrong?

Thanks


OM

Old Pedant
07-12-2012, 01:10 AM
(1) You are using jQuery (or possibly other framework) syntax but you aren't loading the jQuery (or other framework) library.

(2) You are referencing the <div> by id *BEFORE* it is created!



<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="utf-8">
<title>My Development Website</title>
</head>
<body>
<div id="my-name"></div>

<script type="text/javascript">
// plain vanilla JavaScript
document.getElementById("my-name").innerHTML = "Text is here";
</script>
</body>
</html>

Personally, I would recommend you learn straight JavaScript before you go trying to learn jQuery or some other framework(s).

You'll be MUCH better off in the long run.

HINT: Anytime you see code using $("...") notation it's 95% chance it is jQuery, 4% chance some other library, 1% ordinary JS.

OM2
07-12-2012, 05:16 PM
(2) You are referencing the <div> by id *BEFORE* it is created!

doh. ok, that is put down to my inexperience :)


(1) You are using jQuery (or possibly other framework) syntax but you aren't loading the jQuery (or other framework) library.

HINT: Anytime you see code using $("...") notation it's 95% chance it is jQuery, 4% chance some other library, 1% ordinary JS.
erm yes. i didn't realise that was jquery specific! i was copying and reusing other code. for the simplicity of posting my problem here, i cut out a lot of the things that weren't required

thanks for the tip about the $("...") - i had no idead - i just assumed it was all javascript


Personally, I would recommend you learn straight JavaScript before you go trying to learn jQuery or some other framework(s).

You'll be MUCH better off in the long run.

sounds like good advice. where would u suggest is the best starting point?
i want to develop html5 apps for mobiles and make animations and other things - all of which i could do *easily* in flash
i want to code real applications in javascript - so definitely want to get serious about learning

thanks - it's great to learn from experienced coders like u

DrDOS
07-12-2012, 05:39 PM
This is really what javascript is about, getting data from a webpage, changing the webpage, real basics. My advice is don't use a framework until you understand basic code. Not doing it is just like people who use "pagemaker" software of some kind before they learn basic HTML. They inevitably end up making some kind of mistakes that they have no clue about how to correct. And they have no real control over the end product. So, for your own sake, use w3schools or any of the other wonderful resources on the web to take at least a couple of javascript courses before you start using a framework.

OM2
07-12-2012, 05:48 PM
This is really what javascript is about, getting data from a webpage, changing the webpage, real basics. My advice is don't use a framework until you understand basic code. Not doing it is just like people who use "pagemaker" software of some kind before they learn basic HTML. They inevitably end up making some kind of mistakes that they have no clue about how to correct. And they have no real control over the end product. So, for your own sake, use w3schools or any of the other wonderful resources on the web to take at least a couple of javascript courses before you start using a framework.
thanks for the advice
i'm an experienced coder and not a beginner as such
my mistake posted was just a lack of expereince
but yes: i am missing the fundamentals of javascript
w3schools is great, simple, but always a good start

Old Pedant
07-13-2012, 08:39 PM
As Felgall will tell you, the w3cschools stuff is (a) basic and (b) mostly outdated. So use it a as a start, but be ready to get beyond it pretty soon.

You could do worse than following the links from Felgall's sig to some of his articles.

I think sometimes he goes overboard: He just assumes that everyone will be creating JavaScript libraries that have to integrate with every other library in the world and will be used by 31,275 different sites every day. Whereas most of us are creating simple scripts for our own use only. But it can't hurt to use his concepts and often times helps.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum