Advanced Search

Results 1 to 5 of 5

Thread: Change Var

  1. #1
    Join Date
    Feb 2007
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Change Var

    I have a Var, lets call it Var1

    How can i change the value of that var by using a dropdown box?

    Thanks

  2. #2
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I don't know what you want but lets give you an example -

    <script>
    var a = "ape"
    </script>

    This creates a variable called a (as numbers are not aloud to be first in a name) and it has the value of ape...

    <script>
    var a = "ape"
    function changevar() {
    a = "monkey"
    }
    </script>

    All you have to do to change the value is re-write the variable without the var and viola its over-written, a few notes though -

    1. The variable to be edited must be global
    2. Variable names can't start with numbers and only can with a letter or _
    Last edited by pcbrainbuster; 04-22-2007 at 11:44 AM.

  3. #3
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Tabo View Post
    How can i change the value of [a variable] by using a dropdown box?
    Attach a change event listener to the select element, the perform the assignment when the listener is called.

    A simplistic (and rather useless) example would be:

    HTML Code:
    <select onchange="var1 = 'foo';">
    which replaces the value of the variable, var1, with the string, 'foo', when the selection of the element changes.

    Quote Originally Posted by pcbrainbuster View Post
    1. The variable to be edited must be global
    The variable must be in scope. That doesn't necessarily mean global.

    2. Variable names can't start with numbers and only can with a letter or _
    Or a dollar symbol ($), though its use is reserved: "The dollar sign is intended for use only in mechanically generated code." (7.2, ECMA-262 3rd ed.)

    Mike

  4. #4
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Arghhhh.....

    Talk about killing the newbie.
    New coders want someting that works, like a cookbook piece that is clearly explained.

    Code:
    <select onChange="Javascript:foo = this.options[this.selectedIndex].value; alert(foo)">
    <option value="bar" selected>Value to display</option>
    <option value="bar 2">Value to display</option>
    <option value="bar 3">Value to display</option>
    <option value="bar 4">Value to display</option>
    <option value="bar 5">Value to display</option>
    </select>
    
    <script>
    var foo = "";
    
    </script>
    When the select box is changed the onchange event is called. Instead of calling a function, I deal with all the javascript in the select tag by using "Javascript:...."
    The Javascript changes the variable foo to the value of the select box's option that is selected.
    this.options[] = the select box's options
    this.selectedIndex = the index (or order) of the options

    so this.options[3].value would always set the value of foo to bar 4! As index starts at 0!

    Hope this helps explain things



    ps the
    Code:
    ;alert(foo)
    allows you to see what value you are giving to foo. just remove that to make the script working silently

  5. #5
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Bob90 View Post
    New coders want someting that works, like a cookbook piece that is clearly explained.
    Which isn't possible if enough information to provide a definite answer isn't given. The OP can always come back and ask again and provide more information.

    Dishing out answers on a plate doesn't always help. It can just leave the person asking the question dependent upon further help. Whenever possible, I prefer to point people in the right direction and develop things on their own. If they aren't capable of that, they only need say.

    <select onChange="Javascript:foo = this.options[this.selectedIndex].value; alert(foo)">
    That "Javascript:" prefix is unnecessary. Omit it.

    var foo = "";
    There doesn't seem much point in initialising the variable.

    Mike

Bookmarks

Posting Permissions

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