Jump to content

How do I refresh variables on a web page?


kjtocool

Recommended Posts

This may be a simple question:  How do I refresh the variables on a web page, without re-loading the rest of the page?  For example:

 

Say I have this in the head:

<script language="javascript" type="text/javascript">
<!-- 

var movie_1 = "Movie 1";
var movie_2 = "Movie 2";
var movie_3 = "Movie 3";
var movie_4 = "Movie 4";
var movie_5 = "Movie 5";

  function setMovieNames() {
movie_1 = "Movie 7";
movie_2 = "Movie 2";
movie_3 = "Movie 3";
movie_4 = "Movie 4";
movie_5 = "Movie 5";
  }

//-->
</script>

 

And then something like this in the Body:

<script language="javascript" type="text/javascript">
<!-- Prints the #1 Movie Name

document.write(movie_1);

//-->
</script>

<script language="javascript" type="text/javascript">
<!-- Prints the #2 Movie Name

document.write(movie_2);

//-->
</script>

<script language="javascript" type="text/javascript">
<!-- Prints the #3 Movie Name

document.write(movie_3);

//-->
</script>

<script language="javascript" type="text/javascript">
<!-- Prints the #4 Movie Name

document.write(movie_4);

//-->
</script>

<script language="javascript" type="text/javascript">
<!-- Prints the #5 Movie Name

document.write(movie_5);

//-->
</script>

 

 

How do I get it so that the web page will update those variables anytime the function to set the names is called?

Link to comment
Share on other sites

So ... doing a little research, the best option I could come up with was:

 

function setMovieNames() {
  	with (document.form1) {
  		textfield.value = "New";

}
  }

 

First off I had to modify the function like above.  Secondly I had to make all the fields I wanted to update text fields.  And lastly I had to modify the css so that the text boxes are essentially invisible (no background or border).

 

.textbox {
background:none;
border:none;
}

 

I'll continue working, if I find any other ways, I'll update.  Please feel free to give any input, I'm pretty new to javascript.

Link to comment
Share on other sites

instead of text fields, you can just use any element: span, p, div, td, whatever

the, just use innerHTML on it:

 

<span id="movie_1"></span>
<span id="movie_2"></span>
<span id="movie_3"></span>
<span id="movie_4"></span>
<span id="movie_5"></span>

 

function setMovieNames() {
document.getElementById('movie_1').innerHTML = movie_1;
document.getElementById('movie_2').innerHTML = movie_2;
document.getElementById('movie_3').innerHTML = movie_3;
document.getElementById('movie_4').innerHTML = movie_4;
document.getElementById('movie_5').innerHTML = movie_5;
  }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.