473,794 Members | 2,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Begginer ... question ...

gor
I would like to find out how javascript can change some text (string)
inside <DIV> tag with event onClick.
That i want to solve with function callWrite(); ... so you can see in
the code. The thing should be simple, not to me ofcourse.

I dont have a clue, but maybe method getElementById may have somthing to
do with solving my problem. I dont know.
html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Proba</title>
</head>
<style type="text/css">
#plavidiv{
background-color:#0066FF;
width:600;
height:20;
}
</style>

<script language="javas cript">
function callWrite(){
document.write( "i want this to be inside div tag ... but its not!
the .write method seems to erase all html ... ");
}
</script>

<body>
<div id="plavidiv" onClick="zovi() ;">this is the content i want to
malipulate with javascript...</div>
</body>
</html>
Jul 23 '05 #1
9 1437
A made a few changes and it seems to work now.

I changed the callWrite() method and I changed the onClick handler in
the div to call callWrite().

Click the old text to get the new text.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Proba</title>
</head>
<style type="text/css">
#plavidiv{
background-color:#0066FF;
width:600;
height:20;
}
</style>

<script language="javas cript">
function callWrite(){
var mydiv = document.getEle mentById("plavi div");
mydiv.childNode s[0].nodeValue = "i want this to be inside div tag
.... but its not! the .write method seems to erase all html ... ";
}
</script>

<body>
<div id="plavidiv" onClick="callWr ite();">this is the content i
want to
malipulate with javascript...</div>

</body>
</html>
Jul 23 '05 #2
gor
Thanx, but Opera and Firefox doesnt work. I dont know ... did you check
in browser (runtime) ?

So, i prosume .nodeValue is some method to write inside the div tag. ?
..childNodes[0] is the return value of mydiv with index of [0], why
index? Does it means if #plavidiv has more than one <div> tag, so it can
be located? I dont know why this doesnt work.

Sory in my english :)
Jul 23 '05 #3
gor
one more thing, my document is document1.html, on some forums on net I
see that people are talking about XML Node Value, maybe thats why it
doesnt work ? Can it be truth that NodeValue is a XML thing ?
Jul 23 '05 #4
gor@n wrote:
one more thing, my document is document1.html, on some forums on net I see that people are talking about XML Node Value, maybe thats why it
doesnt work ? Can it be truth that NodeValue is a XML thing ?


Calling a non-existent function: bad. Your HTML could use some work,
too. Try this...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/str*ict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<title>Proba</title>
</head>
<style type="text/css">
#plavidiv{
background-color:#0066FF;
width: 600px;
font-size: 140%;
padding: 4px;
cursor: pointer;
}

</style>
<script type="text/javascript">

function callWrite(obj, text)
{
if ('string' == typeof obj)
obj = document.getEle mentById(obj);
if (obj)
{
while (obj.hasChildNo des())
obj.removeChild (obj.lastChild) ;
obj.appendChild (document.creat eTextNode(text) );
}
return false;
}

</script>
<body>
<div id="plavidiv" onclick="callWr ite(this,'I want this to be inside
div tag ... but it...is !')">
this is the content i want to malipulate (?) with javascript...
</div>
<a href="#null" onclick="return callWrite('plav idiv','Now, it says
Hello, world !')">next</a>
</body>
</html>

google "javascript DOM" for more...lots more -

Jul 23 '05 #5
gor@n wrote:
I would like to find out how javascript can change some text (string)
inside <DIV> tag with event onClick.
That i want to solve with function callWrite(); ... so you can see in
the code. The thing should be simple, not to me ofcourse.

I dont have a clue, but maybe method getElementById may have somthing to
do with solving my problem. I dont know.

[...]

For a useful, free, on-line introduction to JavaScript (and HTML,
CSS, etc.) try:

<URL:http://www.w3schools.c om/>

--
Rob
Jul 23 '05 #6
gor
Thnx, but the code you made seems not working in 3 browsers, i dont
know? But this stuff is usefull, and it will help me to study this methods.

In w3schools.com i found great example. But i have to figure it out how
this works with divs.

<html>
<head>
<script type="text/javascript">
function myHref()
{
document.getEle mentById('myAnc hor').innerText ="Visit W3Schools"
document.getEle mentById('myAnc hor').href="htt p://www.w3schools.c om"
}
</script>
</head>

<body>
<a id="myAnchor" href="http://www.microsoft.c om">Visit Microsoft</a>
<form>
<input type="button" onclick="myHref ()" value="Change URL and text">
</form>
</body>

</html>

I have expirience in programming but not in web so I cant belive this "
"simple" expample can be so complicated.

So the .innerText is the method that writes inside the HTML ?

Jul 23 '05 #7
Try this
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<title>Proba</title>
</head>
<style type="text/css">
#plavidiv{
background-color:#0066FF;
width:600;
height:20;
}
</style>

<script language="javas cript">
function callWrite(dest) {
document.getEle mentById(dest). innerText="i want this to be inside
div tag ...";
}
</script>
<body>
<div id="plavidiv" onClick="callWr ite(this.id);"> this is the
content i want to
malipulate with javascript...</div>
</body>
</html>

Some points-- you pass the callWrite the id of the object you want to
write to- makes it slightly more usefully generic. You could of course
add a second parameter for the actual text to change it too. These
sort of methods are becoming an increasingly popular way of changing
content on screen without a server run.

Use getElementById and never be tempted to use document.all. as it's IE
specific.

I personally avoid using childnodes etc.. they're supposed to be the
"right" way of doing things, but IMHO it makes code hard to read and
impossible for someone else to debug. Just my 2p :-)

Mike R.

Jul 23 '05 #8
In article <d3**********@s s405.t-com.hr>, go*******@gmail .com says...
Thanx, but Opera and Firefox doesnt work. I dont know ... did you check
in browser (runtime) ?

So, i prosume .nodeValue is some method to write inside the div tag. ?
.childNodes[0] is the return value of mydiv with index of [0], why
index? Does it means if #plavidiv has more than one <div> tag, so it can
be located? I dont know why this doesnt work.

Sory in my english :)


I have firefox 1.0 and it works for me. I don't have Opera, so I can't
check that.

Check and make sure that the long string literals were not wrapped by
your news reader.

At the end I've attached a new version with shorter string literals
which will be less susceptible to line wrapping.

..nodeValue is a property on objects in a DOM tree. getElementById gets
the object representing the div. The text in the div element is a child
node which you get to with the .childNode array.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Proba</title>
</head>
<style type="text/css">
#plavidiv{
background-color:#0066FF;
width:600;
height:20;
}
</style>

<script language="javas cript">
function callWrite(){
var mydiv = document.getEle mentById("plavi div");
mydiv.childNode s[0].nodeValue = "New Text";
}
</script>

<body>
<div id="plavidiv" onClick="callWr ite();">Old text</div>

</body>
</html>

Jul 23 '05 #9
gor@n wrote:
Thanx, but Opera and Firefox doesnt work. I dont know ... [...]


I know why. Firstly, it is far from being Valid HTML or CSS.
Use <http://validator.w3.or g/> to validate your markup and if it became
Valid, validate your stylesheet.

Secondly, it lacks feature tests on the UA's DOM prior to usage and it
lacks tests on the objects returned prior to access. Use the JavaScript
Console of both UAs to see what goes wrong with the script and use Venkman
in Firefox (ask Google for the XPI) to debug it if necessary.
PointedEars
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1594
by: Mansoor | last post by:
Hi, I'm going to start scripting with PHP. I have a good understading of programming languages and have done it for years. Right now I'm seeking an advice on a book that can get me started quick and straightforward. I am a bit in tight time frame, so books like bible php 5 won't really help me. So thanks in advance.
4
1645
by: Player | last post by:
During my setup install of python, I came across an option stating... Compile python files to, .pyc I left it unchecked for now, should I of done otherwise?? As I am a begginer to this, I didn't no if it was a needed option to be checked or otherwise. I assume by compiling the python files to pyc they would run that little bit faster??
2
243
by: thecamisland | last post by:
Hello, I'm a begginer. Is this going to work? Dim fm As FirstMenu Class TransHeadInfo Public Function Validate() As Boolean ... fm.Show() ...
0
1114
by: thecamisland | last post by:
Hello, I'm a begginer. Is this going to work? Dim fm As FirstMenu Class TransHeadInfo Public Function Validate() As Boolean ... fm.Show() ...
13
1665
by: deshix | last post by:
Hi everyone, can you recommend me the best way to learn the PHP? I'm really begginer...
0
9671
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9518
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10433
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10212
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10161
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6777
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.