disabled property

J

josh

Hi,

I'm doing a very simple task. After a page with Ajax is returning a
resultset I have two variables
containing true or false value to enable/disable first/last buttons
for a navigation bar.

but when I do:

var first = Boolean(nav[1]);
var last = Boolean(nav[2]);

document.getElementById("btn_f").disabled=first;
document.getElementById("btn_b").disabled=first;
document.getElementById("btn_n").disabled=last;
document.getElementById("btn_l").disabled=last;

all the buttons are disabled!

P.S.
If I put manually the value true or false the controls do the job!
P.S. 2
my tags are like this:

<button type="button" id="btn_f"><img src="../images/first.png"
width="16" height="16" /></button>

Thanks
 
R

RobG

Hi,

I'm doing a very simple task. After a page with Ajax is returning a
resultset I have two variables
containing true or false value to enable/disable first/last buttons
for a navigation bar.

What are the "values"?
but when I do:

var first = Boolean(nav[1]);
var last = Boolean(nav[2]);

If you have something like:

var nav = ['true', 'false'];


Then nav[0] and nav[1] will evaluate to true. Using the Boolean
constructor as a function will evaluate the argument and return a
boolean value based on the result. A string other than the empty
string '' will evaluate to true.

Consider:

var nav = ['true', ''];
alert(
'nav[0]: ' + Boolean(nav[0]) +
'\n' +
'nav[1]: ' + Boolean(nav[1])
);
 
J

josh

I'm doing a very simple task. After a page with Ajax is returning a
resultset I have two variables
containing true or false value to enable/disable first/last buttons
for a navigation bar.

What are the "values"?
but when I do:
var first = Boolean(nav[1]);
var last = Boolean(nav[2]);

If you have something like:

var nav = ['true', 'false'];

Then nav[0] and nav[1] will evaluate to true. Using the Boolean
constructor as a function will evaluate the argument and return a
boolean value based on the result. A string other than the empty
string '' will evaluate to true.

Consider:

var nav = ['true', ''];
alert(
'nav[0]: ' + Boolean(nav[0]) +
'\n' +
'nav[1]: ' + Boolean(nav[1])
);

good this is the answer. Thanks.
P.S. I view that there is not a method like parseBoolean... so I could
do this horrible code:

var first = nav[1].indexOf("true") != -1 ? true : false;
var last = nav[2].indexOf("true") != -1 ? true : false;

any other better idea?
 
P

pr

josh said:
P.S. I view that there is not a method like parseBoolean... so I could
do this horrible code:

var first = nav[1].indexOf("true") != -1 ? true : false;
var last = nav[2].indexOf("true") != -1 ? true : false;

any other better idea?

In descending order of usefulness and assuming 'first' relates to nav[0]:

first = /true/.test(nav[0]);

or case-insensitively:

first = /true/i.test(nav[0]);

If you're sure the strings will be exactly "true" or "false":

first = (nav[0] == "true");
first = (nav[0] == true.toString());

case-insensitively again:

first = (nav[0].toLowerCase() == true.toString());

otherwise:

first = (nav[0].indexOf("true") > -1);
first = 1 + nav[0].indexOf("true"); // 0 or > 0
first = 1 + "true".indexOf(nav[0]); // 0 or > 0
first = !!(1 + nav[0].indexOf("true")); // true or false

or if you want to show off:

first = {"true": true, "false": false}[nav[0]];

Just a few to get you started... :)
 
D

Dr J R Stockton

In comp.lang.javascript messaggio <[email protected]
I'm doing a very simple task. After a page with Ajax is returning a
resultset I have two variables
containing true or false value to enable/disable first/last buttons
for a navigation bar.

Multiposting is rude, even if the second copy is encoded in Italian.

If Ajax provides Javascript with the strings "true" or "false", and no
other, then AFAIK the simplest way to get a Javascript Boolean is by
JB = eval(AjaxString)
which will raise an error if it gets a wrong string.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 
J

josh

first = {"true": true, "false": false}[nav[0]];

I understand that first is an object with two properties: "true" and
"false" but
after? I must understand that first will contain a value true/false
after the [nav[0]] evaluation?

bye
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top