Switching between DIVs

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Switching between DIVs

Post by Parody »

I've just started to learn javascript, well, I say learn, I mean I'm picking up snippets as I go along. I'm getting the general idea, but I think I'm trying to do something a little too advanced for me :( , but it's probably really easy for you :D

I'm experimenting with AJAX, but I think that's really beside the point. What I'm trying to do:

There is a form with a text input box. If the box is empty I want the page to display one 'page' under the input box and if not then I want the page to display a different 'page' under the input box. I say 'page' because it's basically completely new content. Then when the user deletes the contents of the box the page will revert back to the original 'page', the one that was showing when the input box was empty to begin with.

My problem:

I can't figure out how to code into the script the check for whether the box is empty or not. I tried:

Code: Select all

if(variable == null)


but that didn't work, because it just doesn't do anything when you enter data into the box. Then I tried by reversing the statement by adding an exclamation mark before the two equal signs which just made the script run as normal, as if the check wasn't present.

I'm guessing the problem lies in the check and that it only checks as soon as the page is loaded. If you can't understand what I'm trying to explain then I'll post the script.

Thanks in advance :D
Last edited by Parody on Fri Oct 05, 2007 10:41 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Post the script and the HTML.

Make sure to use appropriate syntax highlighters. Also, please choose a new subject for this thread.
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:2. Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

These : DOM Input, .value, onkeypress will be useful for you.
There are 10 types of people in this world, those who understand binary and those who don't
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Post by Parody »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Ok, I'll post the script. My problem is though that I don't want this ajaxfunction to run every time the input changes, just every second if the box isn't empty. 

[syntax="html"]<div align=center>
<form name='myForm' style='border:none;'>
 <table id='form_top'>
  <tr><td><input name='q' type='text' class='search_input' id='form_top_input' size='60' maxlength="60"></td></tr>
 </table>
</form>
</div>
<script>
	var checknull = document.myForm.q.value
	if(checknull !== null) {


setInterval ( "ajaxFunction()", 1000 );
}
I don't know how to utilise different javascript functions as of yet. So can someone please explain and correct the code for me please?

Thanks


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Code: Select all

	<input type="text" name="q" onkeyup="alert(this.value !='')">
You''ll also need clearInterval()
There are 10 types of people in this world, those who understand binary and those who don't
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Post by Parody »

Cool, thanks. Any idea how I can implement this into the script? My idea would be:

Code: Select all

Input

setInterval(function to check whether input is empty and if so use the first page, if its not empty then it has to display the second page and run another function
, 1000)
But is it easier to use the onkeyup to do what I want to do with the setinterval? My problem with that is that I don't want the other function to run everytime the input is changed, only every second.

Any ideas? Sorry if I'm asking too much.

Thanks
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Parody wrote:Any ideas? Sorry if I'm asking too much.

Thanks
Feel free to share your code with us :) Then we will help you :)
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply