SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help solving a problem ( custom made pm function )

    Hi,
    Im trying to create my own private message function but im having problem fixing it.
    When you click on the names at the right menu it jumps to the field if you click on it again it adds a new name.
    I would like to block so you cant add more than one of the same name and a maximum of 5 names but im not sure how.
    Im very new to javascript and cant do much with it.
    I would appreciate if someone could help me solve this problem



    Code:
    <li><b><a href="javascript:insertName('Fredrik')">Fredrik</a></b></li><br />
    Code:
    	function insertName(vName){
    		if (document.getElementById('to').value == "") {
    			document.getElementById('to').value = vName;
    		}
    		else {
    			document.getElementById('to').value = document.getElementById('to').value + ", " + vName;
    		}
    	}
    Best Regards
    Trevors

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    Code:
    function insertName(vName){
    	if (document.getElementById('to').value == "") {
    		document.getElementById('to').value = vName;
    	} else {
    
    		// check if the name is already in the list
    		var names = document.getElementById('to').value.split(", ");
    		var found = false;
    		for( var i=0; i < names.length; i++ ) {
    			if( names[i] == vName ) {
    				found = true;
    				break;
    			}
    		}
    
    		if( !found ) {
    			// add the name to the array
    			names[names.length] = vName;
    			// display the names
    			document.getElementById('to').value = names.join(", ");
    		}	else {
    			alert("that name is already in the list");
    		}		
    	}
    }


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
  •