www.webdeveloper.com
Results 1 to 10 of 10

Thread: [RESOLVED] Parse string as Boolean which's actually a Boolean

  1. #1
    Join Date
    Jun 2009
    Posts
    18

    resolved [RESOLVED] Parse string as Boolean which's actually a Boolean

    Hi,

    I read all the stuff parsing strings as booleans. But I could find nothing for my situation.

    For example, I have
    Code:
    ("flag"+frames[i].id)
    which actually represents a real boolean like "flagpbx".


    I can get it's value via
    Code:
    eval("flag"+frames[i].id)


    What I need is the opposite. I need something like
    Code:
    ("flag"+frames[i].id).value=true;


    I tried everything. .bool, .Boolean, Boolean.parse(), etc... Nothing worked.

    Thanks a lot!
    Last edited by Flashbond; 01-30-2014 at 01:56 AM.

  2. #2
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    What exactly are you trying to do? Can you post the entire code?

    Boolean has two possible values true or false. Other values are converted to Boolean so they work like true or false. In general only empty strings works like falsy values.

  3. #3
    Join Date
    Jun 2009
    Posts
    18
    I don't think you've got the whole thing. You don't need the code.
    Think that you have:
    Code:
    var foobar = false, foo="bar";
    alert(eval("foo"+foo)); //* will return false
    All I need is to cast true value to foobar while it is in ("foo"+foo) form.

    It must be something like
    Code:
    Boolean("foo"+foo) = true;
    //* or
    ("foo"+foo).toBoolean = true;
    But somehow I couldn't manage the right expression.
    Last edited by Flashbond; 01-30-2014 at 05:23 AM.

  4. #4
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    Something like this?

    Code:
    var foobar = false, foo="bar";
    alert(eval("foo"+foo+"=true"));

  5. #5
    Join Date
    Jun 2009
    Posts
    18
    No.

  6. #6
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    Maybe my english is bad this morning but I'm not sure are you trying to modify a global variable or you are trying to change the value of the global variable only for a code parsed by the eval function. However, right now I can think of two possible solutions. Hope that one of them is what you're looking for...

    Code:
    var foobar = false, foo="bar";
            
            //First possible solution
            foobar = eval("foo" + foo + "= true; alert(foobar); foobar = false"); //alerts true, foobar must be the last expression
            console.log(foobar); //prints false
            
            
            //Second possible solution
            function x() {
                var foobar = true;
                alert(eval("foo" + foo));
                return;
            }
            
            x() //alerts true
            console.log(foobar) //prints false
    Last edited by tech_soul8; 01-30-2014 at 06:57 AM.

  7. #7
    Join Date
    Jun 2009
    Posts
    18
    Ok, imagine I have several booleans to check if html div styles changed or not:

    Code:
    var flagbiz = true, flagpbx = true, flagspo = true, flagilt = true, frames = document.querySelectorAll('.frame'),
    m = frames.length,frame;
    Then I have some event to flip html divs:

    Code:
    $("#biz").click(function(e){
    flagbiz=flagbiz?false:true;
    flipper (this);
    $("#pbx").click(function(e){
    flagpbx=flagpbx?false:true;
    flipper (this);
    $("#spo").click(function(e){
    flagspo=flagspo?false:true;
    flipper (this);
    $("#ilt").click(function(e){
    flagilt=flagilt?false:true;
    flipper (this);
    });
    and the flip function is:
    Code:
    function flipper (f){
    flipped="#"+f.id; //* set the clicked frame name as jquery understands.
    for(var i = 0; i < m; i++) { //* for every div in html,
    frame="#"+frames[i].id; //* set every frame name in array as jquery understands.
    if (frame==flipped) { //* check if the array element is the clicked div,
    $(flipped).toggleClass("flip"); //* then flip it.
    } else { //* check the other non-clicked divs.
    if (eval("flag"+frames[i].id)==false) { //* already toggled?
    $(frame).toggleClass("flip"); //* then toggele it back to it's old position.
    ("flag"+frames[i].id)=true; //* HERE IS THE PROBLEM LINE. THIS EXPRESSION DOESN'T WORK!
    //* IF THE DIV IS TOGGLED BACK THEN WE MUST SET THE BOOLEAN VALUE TO true AGAIN.
    }}}}
    Last edited by Flashbond; 01-30-2014 at 07:26 AM.

  8. #8
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    You cannot have:

    Code:
    ("flag"+frames[i].id)
    on the left side of an assignment operator because it is not a lvalue.

    If you want to change back the value of a 'flag'*** variables based on the way you have written your code you can do it like this:

    Code:
    eval("flag"+frames[i].id+"=true")
    so, the above code will assign the true value to the one of the 'flag'*** global variables...

  9. #9
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    Eval function is very powerful but its use is almost never neccessary.

    One way you can handle your problem and avoid all of this 'eval' thing is to put your boolean values inside if an object so you can later easly reference them and change their values if neccessary.

    For example:

    Code:
    var flags = {
        flagbiz: true,
        flaghub: true,
        flagsub: true
    }
    Now let's say that we have one div element whose id attribute value is biz.

    You can easly change its associated boolean value like this

    Code:
    flags['flag' + document.getElementById('biz').id] = false
    console.log(flags.flagbiz)
    I hope that I manage to help you out and sorry if there's some error because I'm currently at work.

  10. #10
    Join Date
    Jun 2009
    Posts
    18
    Yeah, also I decided to go with a boolean array.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles