im feeling a bit dumb

jammin's picture

They have: 222 posts

Joined: Sep 2002

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's picture

He has: 3,348 posts

Joined: Jul 2001

I'm not feeling very imaginative right now, so could you post some code up?

Thanks. Wink

jammin's picture

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's picture

He has: 4,048 posts

Joined: Aug 2000

I would suggest using tags and innerHTML()

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Quick note: change "onlcick" to onclick. Wink

The output still won't update, though. I'll have a look in a minute. Wink

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

I've modified your code to use a and innerHTML.

&lt;script language="javascript"&gt;
<!--
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
}

-->
&lt;/script&gt;
</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's picture

He has: 4,048 posts

Joined: Aug 2000

don't forget to run update() onLoad

jammin's picture

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's picture

He has: 3,348 posts

Joined: Jul 2001

May I ask which browser you're using? Wink

The above code works for me in IE6 and NS6.

jammin's picture

They have: 222 posts

Joined: Sep 2002

IE6

Abhishek Reddy's picture

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? Wink

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's picture

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's picture

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's picture

He has: 3,348 posts

Joined: Jul 2001

&lt;script language="javascript"&gt;
<!--
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
      }
   

  }

-->
&lt;/script&gt;
</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>
'

Wink

jammin's picture

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's picture

He has: 3,348 posts

Joined: Jul 2001

The space is added automatically by vB. Wink

You're welcome. Smiling

jammin's picture

They have: 222 posts

Joined: Sep 2002

that space is automatic? weird.

Suzanne's picture

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. Smiling

jammin's picture

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.