473,465 Members | 1,931 Online
Bytes | Software Development & Data Engineering Community
Create 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="javascript">
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 1421
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="javascript">
function callWrite(){
var mydiv = document.getElementById("plavidiv");
mydiv.childNodes[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="callWrite();">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.getElementById(obj);
if (obj)
{
while (obj.hasChildNodes())
obj.removeChild(obj.lastChild);
obj.appendChild(document.createTextNode(text));
}
return false;
}

</script>
<body>
<div id="plavidiv" onclick="callWrite(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('plavidiv','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.com/>

--
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.getElementById('myAnchor').innerText="Vis it W3Schools"
document.getElementById('myAnchor').href="http://www.w3schools.com"
}
</script>
</head>

<body>
<a id="myAnchor" href="http://www.microsoft.com">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="javascript">
function callWrite(dest){
document.getElementById(dest).innerText="i want this to be inside
div tag ...";
}
</script>
<body>
<div id="plavidiv" onClick="callWrite(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**********@ss405.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="javascript">
function callWrite(){
var mydiv = document.getElementById("plavidiv");
mydiv.childNodes[0].nodeValue = "New Text";
}
</script>

<body>
<div id="plavidiv" onClick="callWrite();">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.org/> 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
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...
4
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...
2
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
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
by: deshix | last post by:
Hi everyone, can you recommend me the best way to learn the PHP? I'm really begginer...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.