SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2001
    Location
    Northern Virginia
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    string concatination

    Good morning:
    Here is my problem which seems way to simple, that is why i cannot figure it out.
    I have 12 text boxes labeled alt1 through alt12.
    data will be typed into from 1 to 12 of the boxes. I need to create a string seperated by a comma from these text boxes. I need only the text boxes with data in them to be used. here is what i have so far:

    function passAlt()
    {
    newAlt1 = document.add_alts.alt1.value;
    for (j = 1;j<13;j++)
    {
    newAlt = newAlt1.concat(",").concat(eval("document.add_alts.alt"+(j+1)+".value")
    }
    }

    i don't have any code yet for if a text box is null. this code will always have the first value concatinated to the next value but does not add other boxes. example:
    box1 222
    box2 333
    box3 444

    first time through loop, 222,333
    second time through the loop 222,444
    i need it to be 222,333,444

    please help me

    dan
    Last edited by dfred; Apr 17, 2003 at 05:43.

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Uhh, shooting from the hip here
    Code:
    function passAlt()
    { 
    	var f	   = document.add_alts;
    	var results = [];
    	var i	   = 12;
    	var elem;
    	
    	while( elem = f.elements['alt'+(i--)] )
    	{
    		if ( /\S/.test( elem.value ) )
    		{
    			results.push( elem.value )
    		}
    	}
    	alert( results.join(",") );
    }
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2001
    Location
    Northern Virginia
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it did not work, and since i am doing this in a popup window, i cannot see what the js error is.
    not sure exactly what your js does...could you describe it a bit more so i understand what it is doing?

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Debugged and tested version
    Code:
    <html>
    <head>
    	<title>Test</title>
    	<script type="text/javascript">
    		function passAlt()
    		{
    			var f	   = document.add_alts;
    			var results = [];
    			var i	   = 12;
    			var elem;
    			while( elem = f.elements['alt'+(i--)] )
    			{
    				if ( /\S/.test( elem.value ) )
    				{
    					results.push( elem.value )
    				 }
    			}
    			alert( results.reverse().join("," ) );
    		}
    	</script>
    </head>
    <body>
    	<form name="add_alts">
    		<input type="text" name="alt1" /><br />
    		<input type="text" name="alt2" /><br />
    		<input type="text" name="alt3" /><br />
    		<input type="text" name="alt4" /><br />
    		<input type="text" name="alt5" /><br />
    		<input type="text" name="alt6" /><br />
    		<input type="text" name="alt7" /><br />
    		<input type="text" name="alt8" /><br />
    		<input type="text" name="alt9" /><br />
    		<input type="text" name="alt10" /><br />
    		<input type="text" name="alt11" /><br />
    		<input type="text" name="alt12" /><br />
    		<input type="button" value="Try it" onclick="passAlt()" />
    	</form>
    </body>
    </html>
    If you still want an explanation, however, I can provide that
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2001
    Location
    Northern Virginia
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    when i save it as a file and run it in IE 5.0 service pack 2, there is an error on line 14...
    does not support this property or method
    seems to be this set of code:
    if ( /\S/.test( elem.value ) )
    {
    results.push( elem.value )
    }

    since i am not sure what the /\s/ thing is, i am not really sure how to debug this... can you help?

  6. #6
    SitePoint Wizard davidjmedlock's Avatar
    Join Date
    Dec 2002
    Location
    Nashville, TN USA
    Posts
    1,688
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try:

    Code:
    function passAlt() {
    	var newAlt = "";
    	var temp = "";
    	for (i = 0; i < 13; i++) {
    	   temp = "alt" + i;
    	   newAlt += document.forms[0].elements[temp].value + ",";
    	}
    	return newAlt;
    }

  7. #7
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Argh. Stoopid IE5.0 and it's crappy regular expression handling.

    Try changing it to this

    if ( /^\s*$/.test( elem.value ) )
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  8. #8
    SitePoint Enthusiast
    Join Date
    Sep 2001
    Location
    Northern Virginia
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for all your help...while you were busy trying to help me, i got some help from someone else that gave me this solution:
    function passAlt()
    {
    if (window.opener)
    {
    var i;
    var stAlt;
    var stNewAlt = "";
    var stValue;

    for (i=0; i<add_alts.elements.length; i++)
    {
    if (add_alts.elements[i].type=="text")
    {
    stAlt = add_alts.elements[i].name;
    if (stAlt.substr(0,3) == "alt")
    {
    stValue = add_alts.elements[i].value;
    if (stValue !="")
    {
    if (stNewAlt == "")
    stNewAlt = add_alts.elements[i].value;
    else
    stNewAlt += ", " + add_alts.elements[i].value;
    }
    }
    }
    }
    //add_alts.allAlts.value = stNewAlt;
    if (opener.document.receipt.new_numeric_alt)
    {
    opener.document.receipt.new_numeric_alt.value = stNewAlt;
    }
    }
    window.close();
    }

    now i need to take that value and pass it back to the parent page and add the new value into a select box that is already pre populated.
    as you see i have used an opener. to pass the value from the popup window back to the parent page. the data is there, now i need to get it into an optoin tag so the users can use the new data.


    can any of you help me?

  9. #9
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's a pretty messy solution

    Do you know how to dynamically add option elements to a select?
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  10. #10
    SitePoint Enthusiast
    Join Date
    Sep 2001
    Location
    Northern Virginia
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nope. i am working on it with a co worker, and we have made some progress, but not quite got it

  11. #11
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, basically the process goes like this

    var opt = document.createElement( 'option' );
    opt.text = "text";
    opt.value = "value";
    document.formName.selectName.appendChild( opt );
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  12. #12
    SitePoint Wizard davidjmedlock's Avatar
    Join Date
    Dec 2002
    Location
    Nashville, TN USA
    Posts
    1,688
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    beetle is the DOM god..

  13. #13
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hehe. Well, in case you folks are interested, I made a function a while back to streamline the process of creating DOM objects. You have to be familiar with object-literal syntax to use it, but once you are, it's all cake from there.
    Code:
    document.createHTMLElement = function( elemName, attribs )
    {
    	var elem = document.createElement( elemName );
    	if ( typeof attribs != 'undefined' )
    	{
    		for ( var i in attribs )
    		{
    			switch ( true )
    			{
    				case ( i == 'text' )  : elem.appendChild( document.createTextNode( attribs[i] ) ); break;
    				case ( i == 'class' ) : elem.className = attribs[i]; break;
    				default : elem.setAttribute( i, '' ); elem[i] = attribs[i];
    			}
    		}
    	}
    	return elem;	
    }
    So, using this, creating the sam option can be done with one command, and it satisfies all DOM compatible browsers (for instance, Opera prefers you set the attributes first, then apply a value). Here's that command
    Code:
    var opt = document.createHTMLElement( 'option', { 'text': 'text', 'value': 'value' } );
    I use it all the time. I've even got a couple demos.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





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
  •