SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist S7even's Avatar
    Join Date
    Jun 2002
    Posts
    481
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    assign value to a global varibale

    I have a global varibale:
    var xxxflag=0;

    And the function:
    function func(element){
    flag=element+'flag';
    //code that i need
    }

    Then somewhere in my HTML i have something like:
    <a href="#" id="xxx" onclick="func(this.id)">click</a>

    How can i assign a value of 1 to the xxxflag?
    I tried this:
    flag=1;
    end this:
    eval(flag)=1;
    but non of them works.

  2. #2
    JavaScript Guru (Big Ego) Arielladog's Avatar
    Join Date
    Jul 1999
    Location
    SC, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    From what I understand, you want to first create the name of a variable and then assign a value to that global variable. There are a few ways, but try:


    window[element+'flag'] = 1;

    or:

    eval(element + 'flag = 1');

    or:

    eval(element + 'flag') = 1;

    Something like that or similar should work

    aDog

  3. #3
    SitePoint Evangelist S7even's Avatar
    Join Date
    Jun 2002
    Posts
    481
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The first and second way work.
    Thanks !!


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
  •