Connecting Tech Pros Worldwide Forums | Help | Site Map

Show/Hide text field

FP
Guest
 
Posts: n/a
#1: Jun 26 '06
I'm new to Java Script.
I'm displaying comments people have made. Below each persons' comment
I want to add 2 buttons "Reply" and "Amend". Clicking "Reply" would
display an empty text field below the comment with a spell check &
submit button. Clicking "Amend" would display the same buttons & text
field but pre-populated with the original comment.

Using Java Script how do I show / hide the text field in my list of
comments but have it as a form so that it can be sumitted?
How do I tell it to populate the text field or should I have a second
text field that is pre-populated and show it instead of the first text
field?

Erwin Moller
Guest
 
Posts: n/a
#2: Jun 27 '06

re: Show/Hide text field


FP wrote:
[color=blue]
> I'm new to Java Script.
> I'm displaying comments people have made. Below each persons' comment
> I want to add 2 buttons "Reply" and "Amend". Clicking "Reply" would
> display an empty text field below the comment with a spell check &
> submit button. Clicking "Amend" would display the same buttons & text
> field but pre-populated with the original comment.
>
> Using Java Script how do I show / hide the text field in my list of
> comments but have it as a form so that it can be sumitted?[/color]

Hi,

The easiest way to show/hide some part of the page (form or something else)
is using CSS.
Put the part you want to show/hide in a div, and change the
display-property.

<span
onClick="document.getElementById('myId').style.dis play=none">hide</span>

<span
onClick="document.getElementById('myId').style.dis play=block">show</span>

<div id="myId" style="display:block">
<input type="text" name="bla">
</div>

[color=blue]
> How do I tell it to populate the text field or should I have a second
> text field that is pre-populated and show it instead of the first text
> field?[/color]

I have no idea what you are asking here, sorry.

Regards,
Erwin Moller

FP
Guest
 
Posts: n/a
#3: Jun 28 '06

re: Show/Hide text field


Erwin Moller wrote:[color=blue]
> The easiest way to show/hide some part of the page (form or something else)
> is using CSS.
> Put the part you want to show/hide in a div, and change the
> display-property.
>
> <span
> onClick="document.getElementById('myId').style.dis play=none">hide</span>
>
> <span
> onClick="document.getElementById('myId').style.dis play=block">show</span>
>
> <div id="myId" style="display:block">
> <input type="text" name="bla">
> </div>[/color]


I tried your code and couldn't get it to work. I'm not familiar with
style sheets, nor Java. If possible I would like to do this without
CSS (less things to go wrong). Below is the exact code I tried, please
point out any errors, and keep in mind I'm new to this.
I tried merging your code with other posts I read, all without luck.
The "Span Hide" doesn't act as a button, the "Java Hide" acts like a
button but doesn't do anything.


<HTML>
<SCRIPT LANGUAGE="JavaScript">
function Show(){
document.getElementById( "myId" ).style.display = "block"; }
function Hide(){
document.getElementById( "myId" ).style.display = "none"; }
</script>

<BODY>
<span onClick="document.getElementById('myId').style.dis play=none">Span
Hide</span>
<span
onClick="document.getElementById('myId').style.dis play=block">Span
Show</span>

<input type="button" onclick="Show();" value="Java Show">
<input type="button" onclick="Hide();" value="Java Hide">

<div id="myId" style="display:block">>
<P>Test</P>
</div>

</BODY>
</HTML>

Randy Webb
Guest
 
Posts: n/a
#4: Jun 28 '06

re: Show/Hide text field


FP said the following on 6/28/2006 11:44 AM:[color=blue]
> Erwin Moller wrote:[color=green]
>> The easiest way to show/hide some part of the page (form or something else)
>> is using CSS.
>> Put the part you want to show/hide in a div, and change the
>> display-property.
>>
>> <span
>> onClick="document.getElementById('myId').style.dis play=none">hide</span>
>>
>> <span
>> onClick="document.getElementById('myId').style.dis play=block">show</span>
>>
>> <div id="myId" style="display:block">
>> <input type="text" name="bla">
>> </div>[/color]
>
>
> I tried your code and couldn't get it to work. I'm not familiar with
> style sheets, nor Java. If possible I would like to do this without
> CSS (less things to go wrong).[/color]

You can do it without CSS but CSS makes it a lot simpler. And, Java !=
Javascript.
[color=blue]
> Below is the exact code I tried, please
> point out any errors, and keep in mind I'm new to this.
> I tried merging your code with other posts I read, all without luck.
> The "Span Hide" doesn't act as a button,[/color]

Because it's not a button.
[color=blue]
> the "Java Hide" acts like a button but doesn't do anything.[/color]


It does for me, it hides the div tag. It doesn't actually "hide" it, it
removes it from the flow of the page.
[color=blue]
> <HTML>
> <SCRIPT LANGUAGE="JavaScript">
> function Show(){
> document.getElementById( "myId" ).style.display = "block"; }
> function Hide(){
> document.getElementById( "myId" ).style.display = "none"; }
> </script>
>
> <BODY>
> <span onClick="document.getElementById('myId').style.dis play=none">Span
> Hide</span>[/color]

='none' otherwise it tries to evaluate none as a variable and is
undefined (the error message tells you that)
[color=blue]
> <span
> onClick="document.getElementById('myId').style.dis play=block">Span
> Show</span>[/color]

ditto.


You hide an element one of two ways. You change it's visibility property
or you change it's display property. Which one you use depends on the
effect you want.

<script type="text/javascript">
function changeDisplay(elem,displayMode){
document.getElementById(elem).style.display = displayMode;
}

function changeVisibility(elem,visibilityMode){
document.getElementById(elem).style.visibility = visibilityMode;
}
</script>

HTML:

<div id="div1" style="display: visible">Div 1</div>
<div id="div2" style="visibility: visible">Div 2</div>

<button onclick="changeDisplay('div1','block')">
Make Div1 Visible</button>
<button onclick="changeDisplay('div1','none')">
Make Div1 Hidden</button>

<button onclick="changeVisibility('div2','hidden')">
Make Div2 Hidden</button>
<button onclick="changeVisibility('div2','visible')">
Make Div2 Visible</button>


Have fun.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Erwin Moller
Guest
 
Posts: n/a
#5: Jun 28 '06

re: Show/Hide text field


Randy Webb wrote:
[color=blue]
> FP said the following on 6/28/2006 11:44 AM:[color=green]
>> Erwin Moller wrote:[color=darkred]
>>> The easiest way to show/hide some part of the page (form or something
>>> else) is using CSS.
>>> Put the part you want to show/hide in a div, and change the
>>> display-property.
>>>
>>> <span
>>> onClick="document.getElementById('myId').style.dis play=none">hide</span>
>>>
>>> <span
>>>[/color][/color][/color]
onClick="document.getElementById('myId').style.dis play=block">show</span>[color=blue][color=green][color=darkred]
>>>
>>> <div id="myId" style="display:block">
>>> <input type="text" name="bla">
>>> </div>[/color]
>>
>>
>> I tried your code and couldn't get it to work. I'm not familiar with
>> style sheets, nor Java. If possible I would like to do this without
>> CSS (less things to go wrong).[/color]
>
> You can do it without CSS but CSS makes it a lot simpler. And, Java !=
> Javascript.
>[color=green]
>> Below is the exact code I tried, please
>> point out any errors, and keep in mind I'm new to this.
>> I tried merging your code with other posts I read, all without luck.
>> The "Span Hide" doesn't act as a button,[/color]
>
> Because it's not a button.
>[color=green]
>> the "Java Hide" acts like a button but doesn't do anything.[/color]
>
>
> It does for me, it hides the div tag. It doesn't actually "hide" it, it
> removes it from the flow of the page.
>[color=green]
>> <HTML>
>> <SCRIPT LANGUAGE="JavaScript">
>> function Show(){
>> document.getElementById( "myId" ).style.display = "block"; }
>> function Hide(){
>> document.getElementById( "myId" ).style.display = "none"; }
>> </script>
>>
>> <BODY>
>> <span onClick="document.getElementById('myId').style.dis play=none">Span
>> Hide</span>[/color]
>
> ='none' otherwise it tries to evaluate none as a variable and is
> undefined (the error message tells you that)
>[color=green]
>> <span
>> onClick="document.getElementById('myId').style.dis play=block">Span
>> Show</span>[/color]
>
> ditto.
>
>
> You hide an element one of two ways. You change it's visibility property
> or you change it's display property. Which one you use depends on the
> effect you want.
>
> <script type="text/javascript">
> function changeDisplay(elem,displayMode){
> document.getElementById(elem).style.display = displayMode;
> }
>
> function changeVisibility(elem,visibilityMode){
> document.getElementById(elem).style.visibility = visibilityMode;
> }
> </script>
>
> HTML:
>
> <div id="div1" style="display: visible">Div 1</div>
> <div id="div2" style="visibility: visible">Div 2</div>
>
> <button onclick="changeDisplay('div1','block')">
> Make Div1 Visible</button>
> <button onclick="changeDisplay('div1','none')">
> Make Div1 Hidden</button>
>
> <button onclick="changeVisibility('div2','hidden')">
> Make Div2 Hidden</button>
> <button onclick="changeVisibility('div2','visible')">
> Make Div2 Visible</button>
>
>
> Have fun.
>[/color]

Oops, sorry for none / 'none' confusion.
My bad.
Thanks for correcting Randy.

Regards,
Erwin Moller
FP
Guest
 
Posts: n/a
#6: Jun 29 '06

re: Show/Hide text field


Randy Webb wrote:[color=blue]
> <script type="text/javascript">
> function changeDisplay(elem,displayMode){
> document.getElementById(elem).style.display = displayMode;
> }
>
> function changeVisibility(elem,visibilityMode){
> document.getElementById(elem).style.visibility = visibilityMode;
> }
> </script>
>
> HTML:
>
> <div id="div1" style="display: visible">Div 1</div>
> <div id="div2" style="visibility: visible">Div 2</div>
>
> <button onclick="changeDisplay('div1','block')">
> Make Div1 Visible</button>
> <button onclick="changeDisplay('div1','none')">
> Make Div1 Hidden</button>
>
> <button onclick="changeVisibility('div2','hidden')">
> Make Div2 Hidden</button>
> <button onclick="changeVisibility('div2','visible')">
> Make Div2 Visible</button>[/color]


Thanks, this worked beautifully for me. I see the difference of just
hidding the text vs removing it completely from the page. A working
example is exactly what I needed.



[color=blue]
> ='none' otherwise it tries to evaluate none as a variable and is
> undefined (the error message tells you that)[/color]

This is going to sound like a silly question; when I loaded the page in
Safari I wasn't getting any errors. I'm hosting the page with the
built in Apache on OS X 10.3.9, do I turn on error reporting in Apache,
in Safari, or do I add some HTML code to do that?



[color=blue][color=green]
> > the "Java Hide" acts like a button but doesn't do anything.[/color]
>
> It does for me, it hides the div tag. It doesn't actually "hide" it, it
> removes it from the flow of the page.[/color]

I finally figured out why my initial code, copied from somewhere,
wasn't working. Instead of;
"getElementById" I had "getEmementById".
I think I'm going to be able to sleep better tonight.

Randy Webb
Guest
 
Posts: n/a
#7: Jun 29 '06

re: Show/Hide text field


FP said the following on 6/28/2006 8:13 PM:[color=blue]
> Randy Webb wrote:[/color]

<snip>
[color=blue][color=green]
>> ='none' otherwise it tries to evaluate none as a variable and is
>> undefined (the error message tells you that)[/color]
>
> This is going to sound like a silly question; when I loaded the page in
> Safari I wasn't getting any errors. I'm hosting the page with the
> built in Apache on OS X 10.3.9, do I turn on error reporting in Apache,
> in Safari, or do I add some HTML code to do that?[/color]

You have to turn it on in Safari, although I do not know how to do that.
[color=blue][color=green][color=darkred]
>>> the "Java Hide" acts like a button but doesn't do anything.[/color]
>> It does for me, it hides the div tag. It doesn't actually "hide" it, it
>> removes it from the flow of the page.[/color]
>
> I finally figured out why my initial code, copied from somewhere,
> wasn't working. Instead of;
> "getElementById" I had "getEmementById".
> I think I'm going to be able to sleep better tonight.[/color]

The problem with the original code you posted was none instead of 'none'
and no other errors. If your original had getEmementById then you fixed
it before you posted it.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
FP
Guest
 
Posts: n/a
#8: Jun 29 '06

re: Show/Hide text field


Randy Webb wrote:[color=blue]
> You have to turn it on in Safari, although I do not know how to do that.[/color]

Just thought I would share incase anyone else needs an answer to this.
Quit Safari, from the terminal window enter the command
defaults write com.apple.Safari IncludeDebugMenu 1
this adds a debug menu with some neat features as the right most menu
in Safari


[color=blue]
> The problem with the original code you posted was none instead of 'none'
> and no other errors. If your original had getEmementById then you fixed
> it before you posted it.[/color]

I could have sworn it had the error last night, but after staring at
the computer screen so much I'm starting to wonder if I wasn't seeing
things.

Closed Thread