473,397 Members | 1,961 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,397 software developers and data experts.

get a value if a condition is filled

Hi,

I'm looking for the shortest way to write :

if (form.interface_template.options.length > 0) {
template =
form.interface_template.options[form.interface_template.selectedIndex].value

} else {
template = ''
}
thanks in advance
Jul 23 '05 #1
4 1919
In article <d3**********@news.hispeed.ch>, al***@floor.ch enlightened us
with...
Hi,

I'm looking for the shortest way to write :

if (form.interface_template.options.length > 0) {
template =
form.interface_template.options[form.interface_template.selectedIndex].value

} else {
template = ''
}

hrm...maybe terniary?

template = (form.interface_template.options.length > 0 ?
form.interface_template.options[form.interface_template.selectedIndex].value
: '');

Out of curiosity, why does it matter?

--
--
~kaeli~
What if the Hokey Pokey IS what's it's all about?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #2
Alexandre Jaquet wrote:
Hi,

I'm looking for the shortest way to write :

if (form.interface_template.options.length > 0) {
template =
form.interface_template.options[form.interface_template.selectedIndex].value

} else {
template = ''
}


f=form.interface_template;
template=f.options.length?f[f.selectedIndex].value:"";

Mick
Jul 23 '05 #3
Alexandre Jaquet wrote:
Hi,

I'm looking for the shortest way to write :

if (form.interface_template.options.length > 0) {
template =
form.interface_template.options[form.interface_template.selectedIndex].value

} else {
template = ''
}


There are two things you can do:

Use a variable to store a reference to the form element, it will save
lookups and therefore be faster (but you may not notice the couple of
clock cycles shaved from the execution time if it's a one-of :-) ).

Reduce code bloat. The value of a select is the value of the selected
option, and the value of the selected option is that of the value
attribute, or if it conforms to the W3C spec and doesn't have one,
the content (text). In the case of IE, it must have a value
attribute or 'value' will return an empty string.

<URL:http://www.w3.org/TR/html4/interact/forms.html#edef-SELECT>

Presuming that 'form' is a reference to a form on your page, and that
'interface_template' is the name of a select element in 'form' and
that an option is selected (you should always set one to selected)
and it has a value attribute:

var template;
var t = form.interface_template;
(t.options.length > 0)? template = t.value : '';

will do the trick - though I don't understand what the significance
of testing how many options there are. If there are none, the value
of the select will be empty or ''. Incidentally, that would be
invalid HTML.

So you may as well just write:

var template = form.interface_template.value;
Some play code:

<form action="">
<select name="sel">
<option value="one" selected>one</option>
<option value="two" >two</option>
<option value="three">three</option>
<option value="four" >four</option>
</select>
<br>
<input type="button" value="Selected value" onclick="
alert(this.form.sel.value);
">
</form>
--
Rob
Jul 23 '05 #4
RobG wrote:
[...]

Ughhh, it's late...

var template;
var t = form.interface_template;
(t.options.length > 0)? template = t.value : '';


That last line should have been:

template = (t.options.length > 0)? t.value : '';

But I still think testing t.options.length has no significance in
this context.

[...]

--
Rob
Jul 23 '05 #5

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

Similar topics

2
by: rg | last post by:
Hi all, I have query about passing a parameter from form to a query. My situation is as follows: I have a query that pulls up some data from a table, however there is a condition that must...
21
by: Allin Cottrell | last post by:
OK, I realize that what I am asking here is not likely to have a answer within the C standard. Nonetheless, it is not specific to any particular platform, so I'll hazard the question anyway. A...
1
by: Micheal | last post by:
I have an ASP.NET website using C# that has a Web Control DropDownList. The DropDownList is filled by a database call to SQL Server. The problem is that when I click to change what is selected...
4
by: skanemupp | last post by:
it seems to me from my results that when i use a while-loop it will execute once after the condition is met. ie the conditions is met the code executes one time more and then quits.
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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.