473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to make a method universal

I have the following code which I made work for one item on my page,
now I've got more items and while it still works it takes affect on the
top item not the one that it should. What the code is doing is making a
text box visible and changing the value of something else.

I know what the problem is, and I know that it has to be made universal
to fix it, I just don't know how to do that. When I call the method I
have:
onchange="colourCheck(this.selectedIndex)"
as I need the value of the drop down box.

The method is:

function colourCheck(myForm){
if (myForm == 5){
document.getElementById('os1').style.display = "block";
document.getElementById('on1').value = "Other";
}
else {
document.getElementById('os1').style.display = "none";
}
}

Any help very much appreciated!

Thank you!

Apr 10 '06 #1
6 1000
Flic said on 11/04/2006 9:38 AM AEST:
I have the following code which I made work for one item on my page,
now I've got more items and while it still works it takes affect on the
top item not the one that it should. What the code is doing is making a
text box visible and changing the value of something else.
By 'text box' do you mean an input element of type text? Or is it a div
or span with some text?

I know what the problem is, and I know that it has to be made universal
to fix it, I just don't know how to do that. When I call the method I
have:
onchange="colourCheck(this.selectedIndex)"
as I need the value of the drop down box.

The method is:

function colourCheck(myForm){
if (myForm == 5){
document.getElementById('os1').style.display = "block";


Unless you specifically want 'block', it is better to return the display
attribute to '' (empty string) to allow it to return to the default or
whatever it has been set to by CSS or inheritance.

document.getElementById('os1').style.display = "";
Search the archives for 'display block none'.

Setting an input to display: block is likely not appropriate - the
default CSS 2 display property for an input element is inline-block:

<URL:http://www.w3.org/TR/CSS21/sample.html>
Not all browsers might implement that, so use ''.
[...]

--
Rob
Group FAQ: <URL:http://www.jibbering.com/FAQ>
Apr 11 '06 #2

RobG wrote:
Flic said on 11/04/2006 9:38 AM AEST:
I have the following code which I made work for one item on my page,
now I've got more items and while it still works it takes affect on the
top item not the one that it should. What the code is doing is making a
text box visible and changing the value of something else.


By 'text box' do you mean an input element of type text? Or is it a div
or span with some text?


Sorry I wasn't very clear, I mean an input element of type text.
I know what the problem is, and I know that it has to be made universal
to fix it, I just don't know how to do that. When I call the method I
have:
onchange="colourCheck(this.selectedIndex)"
as I need the value of the drop down box.

The method is:

function colourCheck(myForm){
if (myForm == 5){
document.getElementById('os1').style.display = "block";


Unless you specifically want 'block', it is better to return the display
attribute to '' (empty string) to allow it to return to the default or
whatever it has been set to by CSS or inheritance.

document.getElementById('os1').style.display = "";
Search the archives for 'display block none'.

Setting an input to display: block is likely not appropriate - the
default CSS 2 display property for an input element is inline-block:

<URL:http://www.w3.org/TR/CSS21/sample.html>
Not all browsers might implement that, so use ''.


To be honest, I hadn't even thought of using that, changed now :-)

I realised that to get what I want I would probably need the id similar
to my this.SelectedIndex, but I wouldn't know how to do this as they
are only related to the drop down box with the onChange by being on the
same form.

Apr 11 '06 #3
Flic said on 11/04/2006 10:21 AM AEST:
RobG wrote:

[...]
Unless you specifically want 'block', it is better to return the display
attribute to '' (empty string) to allow it to return to the default or
whatever it has been set to by CSS or inheritance.

document.getElementById('os1').style.display = "";
Search the archives for 'display block none'.

Setting an input to display: block is likely not appropriate - the
default CSS 2 display property for an input element is inline-block:

<URL:http://www.w3.org/TR/CSS21/sample.html>
Not all browsers might implement that, so use ''.

To be honest, I hadn't even thought of using that, changed now :-)

I realised that to get what I want I would probably need the id similar
to my this.SelectedIndex, but I wouldn't know how to do this as they
are only related to the drop down box with the onChange by being on the
same form.


An option has a value and text, you can use both if you like, e.g. use
the value to determine which input to hide and the text to display
something to the user.

You can also use the form's elements collection rather than
getElementById. I have no idea what your form looks like or what you
are doing, but the following example may give you some ideas:
<script type="text/javascript">

function colourCheck(sel)
{
var form = sel.form;
if ( '5' == sel[sel.selectedIndex].value ){
form.os1.style.display = "";
form.on1.value = "Other";
} else {
form.os1.style.display = "none";
}
}
</script>

<form action="">
<select onchange="colourCheck(this);">
<option value="0">Select a value</option>
<option value="2">two</option>
<option value="5">five</option>
</select>
<input type="text" name="os1" value="os1">
<input type="text" name="on1" value="on1">
</form>

--
Rob
Group FAQ: <URL:http://www.jibbering.com/FAQ>
Apr 11 '06 #4

RobG wrote:
Flic said on 11/04/2006 10:21 AM AEST:
RobG wrote:

[...]
Unless you specifically want 'block', it is better to return the display
attribute to '' (empty string) to allow it to return to the default or
whatever it has been set to by CSS or inheritance.

document.getElementById('os1').style.display = "";
Search the archives for 'display block none'.

Setting an input to display: block is likely not appropriate - the
default CSS 2 display property for an input element is inline-block:

<URL:http://www.w3.org/TR/CSS21/sample.html>
Not all browsers might implement that, so use ''.

To be honest, I hadn't even thought of using that, changed now :-)

I realised that to get what I want I would probably need the id similar
to my this.SelectedIndex, but I wouldn't know how to do this as they
are only related to the drop down box with the onChange by being on the
same form.


An option has a value and text, you can use both if you like, e.g. use
the value to determine which input to hide and the text to display
something to the user.

You can also use the form's elements collection rather than
getElementById. I have no idea what your form looks like or what you
are doing, but the following example may give you some ideas:
<script type="text/javascript">

function colourCheck(sel)
{
var form = sel.form;
if ( '5' == sel[sel.selectedIndex].value ){
form.os1.style.display = "";
form.on1.value = "Other";
} else {
form.os1.style.display = "none";
}
}
</script>

<form action="">
<select onchange="colourCheck(this);">
<option value="0">Select a value</option>
<option value="2">two</option>
<option value="5">five</option>
</select>
<input type="text" name="os1" value="os1">
<input type="text" name="on1" value="on1">
</form>

--
Rob
Group FAQ: <URL:http://www.jibbering.com/FAQ>


The code did indeed give me ideas and I modified it to fit my code.
While it works for the first item it doesn't work for the ones after,
coming up as 'value' is null or not an object.

I've uploaded my code so you can see what I am trying to do, it can be
found at:
http://airraid.mancubus.net/flic/exp...necklaces.html
i didn't upload the pictures for the page hence them not displaying.

Apr 11 '06 #5
Flic wrote:
[snip ... old quotes]

The code did indeed give me ideas and I modified it to fit my code.
While it works for the first item it doesn't work for the ones after,
coming up as 'value' is null or not an object.

I've uploaded my code so you can see what I am trying to do, it can be
found at:
http://airraid.mancubus.net/flic/exp...necklaces.html
i didn't upload the pictures for the page hence them not displaying.

In your HTML you have 5 Forms.
Though they look the same, they aren't

The first one has (correct):
<select name="os0" onchange="colourCheck(this)">

The other 4 has (incorrect):
<select name="os0" onchange="colourCheck(this.selectedIndex)">

Make them the same and the script should basically work.

Apr 11 '06 #6

[on] wrote:
Flic wrote:
[snip ... old quotes]

The code did indeed give me ideas and I modified it to fit my code.
While it works for the first item it doesn't work for the ones after,
coming up as 'value' is null or not an object.

I've uploaded my code so you can see what I am trying to do, it can be
found at:
http://airraid.mancubus.net/flic/exp...necklaces.html
i didn't upload the pictures for the page hence them not displaying.

In your HTML you have 5 Forms.
Though they look the same, they aren't

The first one has (correct):
<select name="os0" onchange="colourCheck(this)">

The other 4 has (incorrect):
<select name="os0" onchange="colourCheck(this.selectedIndex)">

Make them the same and the script should basically work.


Thank you for pointing this out. I realised this earlier today along
with something else in the other four that I had to change, but as
quite often happens I couldn't get to the computer to fix it at the
time. It was a silly mistake I know!

Everything working now :-D

Apr 11 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: The Eternal Squire | last post by:
PEP: 336 Title: Make None Callable Version: $Revision: 1.1 $ Last-Modified: $Date: 2004/11/03 16:58:30 $ Author: Andrew McClelland Status: Draft Type: Standards Track Content-Type: text/plain...
0
by: Nick Kew | last post by:
Rationale ========= Many applications today benefit from an SGML and/or XML Entity Catalogue to dereference entities referenced by a Public Identifier. For a validating SGML parser this is an...
8
by: Brett | last post by:
Sorry for all the post on conversion from VB.NET to C#. Just can't figure some of these out. getElementsByTagName method is fine in VB but get this error in C#: Object does not contain a...
44
by: gregory.petrosyan | last post by:
Hello everybody! I have little problem: class A: def __init__(self, n): self.data = n def f(self, x = ????) print x All I want is to make self.data the default argument for self.f(). (I
5
by: EP | last post by:
This inquiry may either turn out to be about the suitability of the SHA-1 (160 bit digest) for file identification, the sha function in Python ... or about some error in my script. Any insight...
0
by: frostbb | last post by:
1st I know about the new DataGridView control. For various reasons we need the IsSelected( ) method of the old DataGrid to work in the upgraded apps. I have a small test app that illustrates the...
2
by: Jeffrey Walton | last post by:
Hi All, BMP Strings are a subset of Universal Strings.The BMP string uses approximately 65,000 code points from Universal String encoding. BMP Strings: ISO/IEC 10646, 2-octet canonical form,...
3
by: raylopez99 | last post by:
I suspect the answer to this question is that it's impossible, but how do I make the below code work, at the point where it breaks (marked below). See error CS0411 This is the complete code. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.