SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict djh's Avatar
    Join Date
    Apr 2000
    Location
    Long Beach, CA
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Input Field magic?

    Hello folks.

    I want to check the value of an input field against an array as an user is typing...

    So in my input field, if I type "A" i want to check that value - A - against my array (or list menu...), and if "AB", then value AB, etc. This seems entirely possible but I don't know if it's possible to do 2 things:

    1. Can you run javascript, or is there an event handler, on an input field for every time a character is typed?

    2. Can you store into an array not the value but the label or text that is between the <option> and </option> tags? I.e., can I retrieve DAVE from <option>DAVE</option>?

    Much props if you know this...

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2003
    Location
    San Diego
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <input type=text onchange="myFunction(this.value)">
    Kyle Maxwell
    www.kylemaxwell.com

  3. #3
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)

    Re: Input Field magic?

    Originally posted by djh

    2. Can you store into an array not the value but the label or text that is between the <option> and </option> tags? I.e., can I retrieve DAVE from <option>DAVE</option>?

    Much props if you know this...
    HTML:
    Code:
    <select name="names" onchange="return getText(this.selectedIndex, this.form, this);">
    <option value="1">DAVE</option>
    <option value="2">SALLY</option>
    <option value="3">JOHN</option>
    <option value="4">SAM</option>
    </select>
    JavaScript:
    Code:
    function getText(in, f, sel) {
     var theText = f.sel.options[in].text;
     alert(theText);
    }
    If you select an option, the text in the option should appear in the alert box, not the value. For example, if SAM is chosen, the alert box will say "SAM", not "4".

  4. #4
    SitePoint Addict djh's Avatar
    Join Date
    Apr 2000
    Location
    Long Beach, CA
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    vinnie,

    yeah that's exactly what i did bro. here's the code...

    Code:
    function textsearch(thestring) {
    	var cResults = document.getElementById("i");
    	var rExp = new RegExp("^"+thestring, "i")	// literal way does not works since variable - use constructor way
    		for(var i = 0; i<cResults.length; i++) {	
    			var tempLabel=document.thismainForm.i.options[i].label;
    			var matchFound = tempLabel.match(rExp);
    				if (matchFound)	{
    						document.thismainForm.i.options[i].selected=true;
    						hideAll();
    						toggleVisible('Layer'+document.thismainForm.i.options[i].value);
    						i=cResults.length;
    		}
    	}
    }
    hey, javascript is pretty cool... i'm so used to vb so was really odd at first but (i guess i say that now cause things are actually working!)

    --dave

  5. #5
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Nice function there! Growing up on VB, I now actually prefer Javascript. Its syntax is more like other languages (i.e. Java, C, C#, PHP) than VB and is just as easy to develop with after a few uses.


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
  •