im feeling a bit dumb
i have a javascript function that adds 1 to a variable called x, then i tell it to write x, and i have a button that calls the add function. but how do i get it to rewrite x when it changes, this should be simple and i dont know why i dont know how to do it.
anyone can do any amount of work provided it isnt the work they are supposed to be doing.
Abhishek Reddy posted this at 05:48 — 9th November 2002.
He has: 3,348 posts
Joined: Jul 2001
I'm not feeling very imaginative right now, so could you post some code up?
Thanks.
jammin posted this at 05:52 — 9th November 2002.
They have: 222 posts
Joined: Sep 2002
<script language="javascript">
</script>
<script language="javascript">
document.write (x)
</script>
not even sure if im doing all this right.
anyone can do any amount of work provided it isnt the work they are supposed to be doing.
Mark Hensler posted this at 06:10 — 9th November 2002.
He has: 4,048 posts
Joined: Aug 2000
I would suggest using tags and innerHTML()
Abhishek Reddy posted this at 06:20 — 9th November 2002.
He has: 3,348 posts
Joined: Jul 2001
Quick note: change "onlcick" to onclick.
The output still won't update, though. I'll have a look in a minute.
Abhishek Reddy posted this at 06:36 — 9th November 2002.
He has: 3,348 posts
Joined: Jul 2001
I've modified your code to use a and innerHTML.
<script language="javascript">
<!--
var x = 2;
function add() {
x++;
update(); // call update HTML output after increment
}
function sub() {
x--;
update(); // call update HTML output after decrement
}
function update()
{
document.getElementById("x_display").innerHTML = x; // change innerHTML attribute of div x_diplay to the new x
}
-->
</script>
</head>
<body>
<table><tr><td>
<!-- This is the location where x will show -->
<div id="x_display"></div>
</td></tr><tr><td align="center">
<input type="button" onclick="javascript:sub()" value="last">
<input type="button" onclick="javascript:add()" value="next">
</td></tr></table>
Mark Hensler posted this at 19:39 — 9th November 2002.
He has: 4,048 posts
Joined: Aug 2000
don't forget to run update() onLoad
jammin posted this at 19:45 — 9th November 2002.
They have: 222 posts
Joined: Sep 2002
doesnt work for me. it says getElementById is null or not an object. isnt there another way to do this without the div?
Abhishek Reddy posted this at 00:38 — 10th November 2002.
He has: 3,348 posts
Joined: Jul 2001
May I ask which browser you're using?
The above code works for me in IE6 and NS6.
jammin posted this at 00:40 — 10th November 2002.
They have: 222 posts
Joined: Sep 2002
IE6
Abhishek Reddy posted this at 04:12 — 10th November 2002.
He has: 3,348 posts
Joined: Jul 2001
I'm not able to recreate that error. Unless - did you alter the code any, or did you just do a copy and paste?
BTW, and innerHTML is the standard way of doing this. Your other options include using a textbox, or images for text, or have the whole page refresh to update the number (which can be done by a server-side script or even just javascript).
jammin posted this at 04:22 — 10th November 2002.
They have: 222 posts
Joined: Sep 2002
i didnt alter the script.
i want something entirely javascript.
last time i tried a total page refresh it set x back to 1. would i need the x=1 and all that in an external script to make the refresh work?
how would the images for text work? something like this?
document.write ("")
anyone can do any amount of work provided it isnt the work they are supposed to be doing.
jammin posted this at 03:39 — 12th November 2002.
They have: 222 posts
Joined: Sep 2002
well i figured out the problem, my computer isnt pasting things off my clipboard right, so i was missing some bits of the script.
but i have another problem.
i want to put this
var max = 4;
if (x > max)
{ x = 1 }
if (x < 1)
{ x = max }
into the code so it doesnt go below 0 or above a set max,
but it doesnt work.
anyone can do any amount of work provided it isnt the work they are supposed to be doing.
Abhishek Reddy posted this at 04:05 — 12th November 2002.
He has: 3,348 posts
Joined: Jul 2001
<script language="javascript">
<!--
var x = 2; // initial value
var max = 4; // ceiling
var min = 1; // floor
function add()
{
x++;
validate(); // check if the number is too high or too low
update(); // call update HTML output after increment
}
function sub()
{
x--;
validate(); // check if the number is too high or too low
update(); // call update HTML output after decrement
}
function update()
{
document.getElementById("x_display").innerHTML = x; // change innerHTML attribute of div x_diplay to the new x
}
function validate()
{
if(x >= max) // is x too high?
{
x = max; // yes, so reset it to ceiling value
}
else if(x <= min) // is x too low?
{
x = min // yes, so reset it to floor value
}
else // is x just right?
{
x = x; // yes, so leave it as is
}
}
-->
</script>
</head>
<body onload="javascript:update()">
<table><tr><td>
<!-- This is the location where x will show -->
<div id="x_display"></div>
</td></tr><tr><td align="center">
<input type="button" onclick="javascript:sub()" value="last">
<input type="button" onclick="javascript:add()" value="next">
</td></tr></table>
jammin posted this at 15:51 — 12th November 2002.
They have: 222 posts
Joined: Sep 2002
... wow now i really am feeling dumb, i cant beleive i forgot i could put it in a function.
and just to let you know i get an error when you put "java script:add()" for the buttons. you dont need the space in there.
thanks though.
anyone can do any amount of work provided it isnt the work they are supposed to be doing.
Abhishek Reddy posted this at 04:25 — 13th November 2002.
He has: 3,348 posts
Joined: Jul 2001
The space is added automatically by vB.
You're welcome.
jammin posted this at 22:48 — 13th November 2002.
They have: 222 posts
Joined: Sep 2002
that space is automatic? weird.
Suzanne posted this at 01:51 — 14th November 2002.
She has: 5,507 posts
Joined: Feb 2000
it keeps people from running scripts that will break or damage the forums. Not so weird in that respect.
jammin posted this at 03:04 — 14th November 2002.
They have: 222 posts
Joined: Sep 2002
i guess.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.