www.webdeveloper.com
Results 1 to 9 of 9

Thread: Concatenating 2 variables as one

  1. #1
    Join Date
    Apr 2012
    Posts
    17

    Concatenating 2 variables as one

    Hello guys, sometimes I get stuck in simple matters while coding... this is one of them. OK, very simple:

    Code:
    function test(objID1, objID2, objID3, objID4) {
    for (i=1; i<5; i++) {
    alert('The object:' + objID[i] + 'is stupid');
    }
    Is this possible? Joining in two variables as one?

    Thanks.

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,380

    Question

    Does not make sense...

    1. Are objID1 ... ObjID4 arrays? Or are they simple strings?

    Need more information.

  3. #3
    Join Date
    Apr 2012
    Posts
    17
    Quote Originally Posted by JMRKER View Post
    Does not make sense...

    1. Are objID1 ... ObjID4 arrays? Or are they simple strings?

    Need more information.
    Hi JMRKER
    simple strings.

    This is just a question if it is possible to join a variable names with other.

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,380
    Still not sure what you are trying to accomplish...
    Code:
    <!DOC HTML>
    <html>
    <head>
    <title> Untitled </title>
    <script type="text/javascript">
    // do you mean this
    var var1 = 'abc';
    var var2 = 'def';
    alert('var1+var2 = '+var1+var2);
    
    // or do you mean
    var var12 = var1 + var2;
    alert('var12 = '+var12);
    
    // or are you trying to do something like this...
    window[var1+var2] = var1+var2;
    alert('abcdef = '+window[var1+var2]);
    
    // finally, this...
    window['var12'] = var1+var2;
    alert(window['var12']);
    
    // or something else???
    
    </script>
    
    
    </head>
    <body>
    
    </body>
    </html>

  5. #5
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    In this particular test of yours I would recommend accessing the variables via the arguments object instead:

    Code:
    function test()
    {
    	var i;
    	for (i = 0; i < arguments.length; ++i) {
    		alert('The object: ' + arguments[i] + ' is stupid');
    	}
    }
    
    test('one', 'two', 'three');
    JavaScript: Learn | Validate | Compact | bionoid

  6. #6
    Join Date
    Apr 2012
    Posts
    17
    Let's put it like this:

    Code:
    function funcTest(objID1, objID2, objID3, objID4, objID5, objID6, objID7, objID8, objID9, objID10) {
    
    	var arrObjects=[];
    	for (i=0; i<10; i++) {
    		arrObjects[i]=objID[i];
    		}
    
    }

  7. #7
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,380

    Question

    Quote Originally Posted by fowler1979 View Post
    Let's put it like this:

    Code:
    function funcTest(objID1, objID2, objID3, objID4, objID5, objID6, objID7, objID8, objID9, objID10) {
    
    	var arrObjects=[];
    	for (i=0; i<10; i++) {
    		arrObjects[i]=objID[i];
    		}
    
    }
    Have you tried using post #5?

    If yes, what was the problem orwhat error are you getting?

    How does your code differ from this test?
    Code:
    <!DOC HTML>
    <html>
    <head>
    <title> Untitled </title>
    <script type="text/javascript">
    
    function $_(IDS) { return document.getElementById(IDS); }
    
    function funcTest() {
      var arrObjects=[];
      for (var i = 0; i < arguments.length; ++i) { arrObjects[i]=arguments[i]; }
    
      alert(arrObjects.join('\n'));
    }
    </script>
    
    </head>
    <body>
    <input type="text" id="TBox" value="Text Box">
    <p>
    <input type="radio" name="RBtn" id="RBtn0" value="0">RBtn 1
    <input type="radio" name="RBtn" id="RBtn1" value="1">RBtn 2
    <input type="radio" name="RBtn" id="RBtn2" value="2">RBtn 3
    <input type="radio" name="RBtn" id="RBtn3" value="0">RBtn 1
    <input type="radio" name="RBtn" id="RBtn4" value="1">RBtn 2
    <input type="radio" name="RBtn" id="RBtn5" value="2">RBtn 3
    <p>
    <input type="checkbox" id="CBox0" name="CBox0" value="A">CBox A
    <input type="checkbox" id="CBox1" name="CBox1" value="B">CBox B
    <p>
    <select id="SBox" name="SBox" id="SBox">
     <option value="">Pick</option>
     <option value="1">1</option>
     <option value="2">2</option>
     <option value="3">3</option>
     <option value="4">4</option>
     <option value="5">5</option>
     <option value="6">6</option>
    </select>
    <p>
    <button
     onclick="funcTest($_('TBox').id, $_('RBtn0').id, $_('RBtn1').id, $_('RBtn2').id, $_('RBtn3').id, $_('RBtn4').id, $_('RBtn5').id,  $_('CBox0').id, $_('CBox1').id,  $_('SBox').id)">Show IDs</button>
    </body>
    </html>
    Last edited by JMRKER; 08-17-2012 at 08:38 AM. Reason: Added example.

  8. #8
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    Quote Originally Posted by bionoid View Post
    In this particular test of yours I would recommend accessing the variables via the arguments object instead:

    Code:
    function test()
    {
    	var i;
    	for (i = 0; i < arguments.length; ++i) {
    		alert('The object: ' + arguments[i] + ' is stupid');
    	}
    }
    
    test('one', 'two', 'three');
    win.
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

  9. #9
    Join Date
    Apr 2012
    Posts
    17
    Quote Originally Posted by JMRKER View Post
    Does not make sense...

    1. Are objID1 ... ObjID4 arrays? Or are they simple strings?

    Need more information.
    Nevermind guys, I've completely redesigned my code with arrays.
    Thanks anyway.

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