473,407 Members | 2,312 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,407 software developers and data experts.

changing css bu javascript for all input boxes with the same name

Hi all,

I know I'm doign something really daft, but I can't get this to
work...

I have a form with a bunch of inputs called ship_owner[] - why the
[]? Because I'm submitting this page though php and the [] put the data
into an array in the post.... anywhat.

I have a link <a href="javascript:change_class()" >Block mode</a> to
call the following function that's in my head section.
<script>
function change_class(var_type)
{
for (i=0; i<document.getElementsByName('ship_owner[]').length; i++)
{

document.getElementsByName('ship_owner[]')[i].style.className="input_to_text";
}
}
</script>
I have the following style sheet also in my head.

<style>
<!--
input_to_text {
border: 1px solid #000;
background: #987;
padding: 1px;
Font-family: Veranda;
font-size: 10px;
color: #fff;
font-weight: bold;
}
</style>
When I click the link my input boxes aren't changing at all......

help :-)

Many many thanks

Amy

May 5 '06 #1
13 2991
ASM
am*******@gmail.com a écrit :

<script>
function change_class(var_type)
{
// if your form is the 1st in your file what say these alerts ?
alert('dom = '+document.getElementsByName('ship_owner[]').length);
alert('js = '+document.forms[0].elements['ship_owner[]'].length);
for (i=0; i<document.getElementsByName('ship_owner[]').length; i++)
{

document.getElementsByName('ship_owner[]')[i].style.className="input_to_text";
}
}
</script>
I have the following style sheet also in my head.

<style>
<!--
input_to_text {
border: 1px solid #000;
background: #987;
padding: 1px;
Font-family: Veranda;
font-family: Veranda;
font-size: 10px;
color: #fff;
font-weight: bold;
}
/* don't forget --> in end */

-->
</style>



--
Stephane Moriaux et son [moins] vieux Mac
May 5 '06 #2
am*******@gmail.com wrote:
Hi all,

I know I'm doign something really daft, but I can't get this to
work...

I have a form with a bunch of inputs called ship_owner[] - why the
[]? Because I'm submitting this page though php and the [] put the data
into an array in the post.... anywhat.

I have a link <a href="javascript:change_class()" >Block mode</a> to
call the following function that's in my head section.
<script>
function change_class(var_type)
{
for (i=0; i<document.getElementsByName('ship_owner[]').length; i++)
{

document.getElementsByName('ship_owner[]')[i].style.className="input_to_text";

That should be

var inputs = document.getElementsByName("ship_owner[]");
for (var i = 0; i < inputs.length; i++)
inputs[i].className = "input_to_text";
1. "className" is a property of the Element, not it's style property.

2. Avoid multiple calls to functions just for efficiency. <span
class="GreybeardDeveloper">(Don't programmers think about things like
this nowadays?)</span>
}
</script>
I have the following style sheet also in my head.

<style>
<!--
input_to_text {
border: 1px solid #000;
background: #987;
padding: 1px;
Font-family: Veranda;
font-size: 10px;
color: #fff;
font-weight: bold;


Try putting it into your document then!

;-)

......

Nige
May 6 '06 #3
am*******@gmail.com wrote:
<snip>
I have a link <a href="javascript:change_class()" >Block mode</a>

<snip>

Do not ever use javascript pseudo-protocol HREFs as Windows IE (and a
few other browsers) takes their activation as representing navigation,
and that puts the current page into a 'waiting' state, pending its
replacement. In that 'waiting' state various previously functional
browser features stop working. Basically, once a user clicks such a link
all bets are off as to what the browser may subsequently do. Use a
default-action cancelling onclick handler to trigger the function
instead.

Richard.
May 6 '06 #4
ASM wrote:
am*******@gmail.com a écrit :

<snip>
<style>
<!--
input_to_text {
border: 1px solid #000;
background: #987;
padding: 1px;
Font-family: Veranda;


font-family: Veranda;


Should not matter, as CSS property names used in STYLE elements are case
insensitive.
font-size: 10px;
color: #fff;
font-weight: bold;
}


/* don't forget --> in end */


Or forget the <!-- at the beginning, as it is redundant.

Richard.
May 6 '06 #5
Hi guys;

I'm not used to use same names for elements, but shouldn't it be

<script>
....
for (i=0; i<document.getElementsByName('ship_owner').length ; i++)
{

document.getElementsByName('ship_owner')[i].style.className="input_to_tex*t";

}
....</script>

instead? I removed the '[]' from the function calls. I believe by using
just the name itself the function will return an array.

Too bullshitty?

May 8 '06 #6
Marcello said the following on 5/7/2006 8:25 PM:
Hi guys;

I'm not used to use same names for elements, but shouldn't it be
It is used in PHP where any elements that have a common name of
fieldName[], when submitted, are automatically put into an Array for
processing.
<script>
....
for (i=0; i<document.getElementsByName('ship_owner').length ; i++)
{

document.getElementsByName('ship_owner')[i].style.className="input_to_tex*t";

}
No, as the name is not ship_owner, but rather it is ship_owner[] so that
when the page gets submitted to PHP it is already an array on the server
so that the programmer doesn't have to create an array from it.
....</script>

instead? I removed the '[]' from the function calls. I believe by using
just the name itself the function will return an array.
It won't, test it.
Too bullshitty?


It is worse than that.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 8 '06 #7
Hi guys,

Thanks for all the advice - especially TheBagbournes for reminding me
the .style bit was wrong.

BTW you also said... "Avoid multiple calls to functions just for
efficiency." - groovy, how would you go about this mission - i.e.
changing the style of all input boxes called 'ship_owner[]'?

Richard Cornford said that calling the function from the href wasn't
really the thing to do, so I've gone back to the classic <a href="#"
onClick="change_class('block')" >Block mode</a> - is this still
considered ok?

Now, I've done all you have suggested and I still can't get this to
work..... below is my page in it's entirety, if you good people would
cast your eye over it and tell me what daftness I've come up with I
would be most appreciative! :-)

Thanks :-)

Amy - getting rather annoyed with herself and this page!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">

<style>
<!--
input_to_text {
background: pink;
}
-->
</style>
<script>

function change_class(var_type)
{
// both of these alerts give me 7
alert('dom = '+document.getElementsByName('ship_owner[]').length);
alert('js = '+document.forms[0].elements['ship_owner[]'].length);

if (var_type == 'block')
{
alert('proof I get to here')

var inputs = document.getElementsByName("ship_owner[]");
for (var i = 0; i < inputs.length; i++)
{
inputs[i].className = "input_to_text";
}

}
else if (var_type == 'prop')
{
// not yet implemented, but will turn the boxes back to boxes
alert('place-holder');
}
}
</script>
</head>
<body>

<form name="ship_reords" action="block_action.php" method="post">

<table border="1">
<tr>
<th rowspan=1>Ship Name</th>
<th>Ship Owner</th>
</tr>
<tr>
<td>Alone Hammerhead</td>
<td><input name="ship_owner[]" class="" value="Amy" /></td>
</tr>
<tr>
<td>Ambitious Bluegill</td>
<td><input name="ship_owner[]" value="Kate" /></td>
</tr>
<tr>
<td>Dear Angelfish</td>
<td><input name="ship_owner[]" value="Kate" /></td>
</tr>
<tr>
<td>Decent Bullhead</td>
<td><input name="ship_owner[]" value="Crew boat?" /></td>
</tr>
<tr>
<td>Fat Carp</td>
<td><input name="ship_owner[]" value="Amy" /></td>
</tr>
<tr>
<td>Fatty Eel</td>
<td><input name="ship_owner[]" value="Kate (& shop coffers?)"
/></td>
</tr>
<tr>
<td>Free Sprat</td>
<td><input name="ship_owner[]" value="Flag Fleet" /></td>
</tr>
</table>

<a href="#" onClick="change_class('block')" >Block mode</a>

<input type="submit" value="submit"/>
</form>

</body>
</html>

May 8 '06 #8
>hits head on desk repeatedly<

if I add a . before input_to_text in my style definition it all works!

<style>
<!--
..input_to_text {
background-color: pink;
}
-->
</style>

Thank you all for all your help :-)

Amy - relieved

May 8 '06 #9
am*******@gmail.com said the following on 5/8/2006 4:12 AM:
Hi guys,

Thanks for all the advice - especially TheBagbournes for reminding me
the .style bit was wrong.

BTW you also said... "Avoid multiple calls to functions just for
efficiency." - groovy, how would you go about this mission - i.e.
changing the style of all input boxes called 'ship_owner[]'?

Richard Cornford said that calling the function from the href wasn't
really the thing to do, so I've gone back to the classic <a href="#"
onClick="change_class('block')" >Block mode</a> - is this still
considered ok?


After you click the link, watch the location bar. It will append # to
the URL. You can prevent that by returning false from the onclick event
handler:

onclick="change_class('block');return false"

Or better:

onclick="return change_class('block')"

And have the function return true or false.

If the only purpose of the element is to invoke a JS function, use a
button instead:

<button onclick="change_class('block')">Block Mode</button>

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 8 '06 #10
am*******@gmail.com wrote:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<URL:http://www.w3.org/QA/Tips/good-titles>

And a bit more indentation would be better.
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
Can/should be omitted if served via HTTP at first; send the correct
HTTP header instead, it will take precedence over this anyway.
<style>
The `type' attribute is required for `style' and `script' elements:

<style type="text/css">

See also <URL:http://validator.w3.org/>.
<!--
input_to_text {
background: pink;
This color name is not yet part of a Web standard, and it is intended for
for SVG, not HTML:

<URL:http://www.w3.org/TR/css3-color/#svg-color>

Do not rely on it. (The color value it represents is not Truly Web-safe
anyway; the nearest Truly Web-safe color value is #fcc.)

If you declare only the background color, use `background-color' instead
of `background'. And if you declare the background color, declare the
foreground colors as well (and vice-versa):

<URL:http://www.w3.org/QA/Tips/color>
}
-->
</style>
<script>
See above.

<script type="text/javascript">
function change_class(var_type)
{
// both of these alerts give me 7
Do not indent your code using tabs (at least not when posting), use
multiples of 2 or 4 spaces.

[fixed indentation] alert('dom = '+document.getElementsByName('ship_owner[]').length);
alert('js = '+document.forms[0].elements['ship_owner[]'].length);

if (var_type == 'block')
{
alert('proof I get to here')
var inputs = document.getElementsByName("ship_owner[]");
Since this relies on W3C DOM Level 1, it will break in older
user agents, although it could have been avoided (see below):

function change_class(o, var_type)
{
if (o && o.form && o.form.elements)
{
if (var_type == 'block')
{
// ...
var inputs = o.form.elements["ship_owner[]"];
if (inputs)
{
for (var i = 0; i < inputs.length; i++)
{
inputs[i].className = "input_to_text";
}
Since order does not matter here,

for (var i = inputs.length; i--;)
{
inputs[i].className = "input_to_text";
}

is even more efficient. Be sure to feature-test the `className'
property before you access it.
[...]
<form name="ship_reords" action="block_action.php" method="post">
Your `form' element probably does not need a name.
See <news:18****************@PointedEars.de>, a.o.
<table border="1">
Use only CSS for formatting, not deprecated format attributes.
[...]
<tr>
<td>Alone Hammerhead</td>
<td><input name="ship_owner[]" class="" value="Amy" /></td>
This is declared HTML, not XHTML. Omit the `/'. (In HTML, `<... />' is
equivalent to `<...>&gt;' as per built-in SHORTTAG feature. It is only
that _Web browsers_ (only a subset of HTML user agents) tend to implement
HTML insufficiently, and then do error correction; avoid that.)
[...]
<a href="#" onClick="change_class('block')" >Block mode</a>
You need to cancel the click event, else navigation to the undefined target
`#' will take place:

<a href="#" onClick="change_class('block'); return false;">Block mode</a>

However, this feature will not work without client-side script support at
all. So you should create the link with client-side scripting:

<script type="text/javascript">
document.write('<a href="#"'
+ ' onclick="change_class(this, \'block\'); return false;"'
+ '>Block mode<\/a>');
</script>

A (formatted) button would be better suited for the task, though:

<script type="text/javascript">
document.write('<input type="button"'
+ ' onclick="change_class(this, \'block\');"'
+ '>Block mode<\/a>');
</script>
<input type="submit" value="submit"/>

^
See above. And I got the idea that control labels should start uppercase.
PointedEars
--
Multiple exclamation marks are a sure sign of a diseased mind.
-- Terry Pratchett
May 17 '06 #11
Thomas 'PointedEars' Lahn posted the following on 17th of May in the
year 2006 of the proleptic Gregorian Calendar in reply to a post that is
ancient in terms of Usenet. Yet, in his almighty moment of ignorance
though it important to post the following:
am*******@gmail.com wrote:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<URL:http://www.w3.org/QA/Tips/good-titles>

And a bit more indentation would be better.


Irrelevant to the question.
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">


Can/should be omitted if served via HTTP at first; send the correct
HTTP header instead, it will take precedence over this anyway.


Irrelevant to the question.
<style>


The `type' attribute is required for `style' and `script' elements:

<style type="text/css">

See also <URL:http://validator.w3.org/>.


Decent information but guess what? Yep: Irrelevant to the question.
<!--
input_to_text {
background: pink;


This color name is not yet part of a Web standard, and it is intended for
for SVG, not HTML:

<URL:http://www.w3.org/TR/css3-color/#svg-color>

Do not rely on it. (The color value it represents is not Truly Web-safe
anyway; the nearest Truly Web-safe color value is #fcc.)

If you declare only the background color, use `background-color' instead
of `background'. And if you declare the background color, declare the
foreground colors as well (and vice-versa):

<URL:http://www.w3.org/QA/Tips/color>


Again, decent information but guess what? Right again:
Irrelevant to the question.

And, OT to comp.lang.javascript
}
-->
</style>
<script>


See above.

<script type="text/javascript">


Irrelevant to the question.
function change_class(var_type)
{
// both of these alerts give me 7


Do not indent your code using tabs (at least not when posting), use
multiples of 2 or 4 spaces.


Use whatever indentation you want, as long as it doesn't wrap when posted.
[fixed indentation]
alert('dom = '+document.getElementsByName('ship_owner[]').length);
alert('js = '+document.forms[0].elements['ship_owner[]'].length);

if (var_type == 'block')
{
alert('proof I get to here')
var inputs = document.getElementsByName("ship_owner[]");


Since this relies on W3C DOM Level 1, it will break in older
user agents, although it could have been avoided (see below):


<snipped Thomas' attempt at coding>
[...]
<form name="ship_reords" action="block_action.php" method="post">


Your `form' element probably does not need a name.
See <news:18****************@PointedEars.de>, a.o.


<sigh> Once again: Irrelevant to the question.
<table border="1">


Use only CSS for formatting, not deprecated format attributes.


Ditto.
Irrelevant to the question.
OT in comp.lang.javascript.

<snipped the rest of his ramblings, as 99% of it is irrelevant to the
question and OT to comp.lang.javascript>

P.S. Did de.comp.* die down to cause him to return here?

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 17 '06 #12
JRS: In article <zu********************@comcast.com>, dated Wed, 17 May
2006 19:31:54 remote, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
Thomas 'PointedEars' Lahn posted the following on 17th of May in the
year 2006 of the proleptic Gregorian Calendar in reply to a post that is
ancient in terms of Usenet.


Incorrect.

In the British Empire and Colonies, which means you, the proleptic part
of the Gregorian calendar does not apply after the end of 1752-09-13
pGc.

In the regions most directly under Papal influence, it does not apply
after 1582-10-14 pGc.

In the Lahnish regions, the change was generally completed by early
1700.

In Protestant Germany, 1700 was very much not a leap year; February
finished after the 18th.

But it's good to see that your system allows you to use non-FFF dates in
attributions.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
May 18 '06 #13
Dr John Stockton said the following on 5/18/2006 5:27 PM:
JRS: In article <zu********************@comcast.com>, dated Wed, 17 May
2006 19:31:54 remote, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
Thomas 'PointedEars' Lahn posted the following on 17th of May in the
year 2006 of the proleptic Gregorian Calendar in reply to a post that is
ancient in terms of Usenet.
Incorrect.

In the British Empire and Colonies, which means you, the proleptic part
of the Gregorian calendar does not apply after the end of 1752-09-13
pGc.


The USA is part of some "Colonies"? Surely you jest. No part of it has
been a Colony for over 225 years. Or is that an ancient term you are using?
But it's good to see that your system allows you to use non-FFF dates in
attributions.


My system allows me to use any date format I choose and it uses the
format I tell it to use.

But, all in all, thanks for my daily laughter.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 19 '06 #14

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

Similar topics

2
by: John Ryan | last post by:
I've a small bit of javascript on my site that has a from with 2 selection boxes, when you choose an option in the first box, the second one re-populates its list accordingly. But the second...
19
by: dmiller23462 | last post by:
Hi guys....I have absolutely NO IDEA what I'm doing with Javascript but my end result is I need two text boxes to stay hidden until a particular option is selected....I've cobbled together the...
4
by: Marc | last post by:
Is there a way to produce 3 radio buttons, which when one is selected, enables 11 check boxes which are otherwise disabled (greyed out)? EXAMPLE: Option 1 --Radio buttons Option 2 Option 3 ...
5
by: Allan M. | last post by:
I have a series of select boxes that must be populated client side, because they interact with each other. The design specification calls for these boxes to be updated without having to make a...
7
by: wkeithwork | last post by:
I'm pulling my hair out... I have a few radio boxes where a user can choose different options and I want to show an explanation of each option after it's clicked. Here's what I have: <script...
4
tolkienarda
by: tolkienarda | last post by:
hi all I am working on a php driven database program for a literacy program, it will allow them to keep track of classes and students, the part i am strugling with is adding new classes, the...
15
by: fanchun | last post by:
I already built 2 javacript files factor.js and parm.js. factor.js is an array, having several factor as elements. for example, factor.js looks like: var factor= parm.js is also an array, having...
1
by: lolcat | last post by:
Hi guys, new here and to javascript, I have this form that I am trying to you javascript to validate the multiple input text boxes. form as follows <form action="login.php" method="post"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.