SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How can I test this?

    I just wrote up this code to find out whether or not text is selected. This is the whole file, I need help calling the function. Im guessing I need to set up an input button. This is my first time doing anything with javascript besides an alert, any help is appreciated.

    Code:
    <script language="JavaScript" type="text/javascript">
    <!--
    function checkSelection(field) {
    	//IE
    	if (document.all) {
    		//Text is Selected
    		if (strSelection!="") {
    			alert ("IE: Text is Selected")
    		}
    		//Text is not Selected
    		else {
    			alert ("IE: Text is not Selected")
    		}
    	}
    	//FF
    	else if (document.getElementById) {
    		//Text is Selected
    		if(field.selectionStart - field.selectionEnd != 0){
    			alert ("Firefox: Text is Selected")
    		}
    		//Text is not Selected
    		else {
    			alert ("Firefox: Text is not Selected")
    		}
    	}
    }
    //-->
    </script>
    
    <textarea name="testfield">
    Some sample teext for testing.
    </textarea>
    Thanks,
    e39m5

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,014
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)

    events

    Javascript can be used to handle several "events" onload, onclick, onchange, etc. Some are not supported by all browsers, so if this is for online use it's best to use events that (almost) all support. http://www.w3schools.com/jsref/jsref_events.asp
    You could put the javascript in the element tag like
    HTML Code:
    <textarea name="testfield" onselect="checkSelection();">
    or you could assign an event handler to the element in the head script with something like
    HTML Code:
    document.getElementById("testfield").onselect = checkSelection;

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!

    I got it set up using an image with onclick="checkSelection(testfield);" except the function isn't working. Am I calling the function correctly with checkSelection(testfield)? I know the function is running because I put an alert before all of the if statements and it came up.

    e39m5

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,014
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)

    img onclick

    When you click on the image, the text loses focus and is therefore no longer selected. Did you get a "not selected" alert? Try this instead
    HTML Code:
    <textarea name="testfield" onselect="checkSelection(this);">

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No i did not get a "not selected" alert. I put the onselect="checkSelection(this); and it didn't do much of anything, except i cant seem to highlight more than one charcter by clicking and dragging.

    Also, why would I put this on the textarea? I know that in the end I am going to want to click an image to call the function.

    If it loses the selection how do bbcode editors work where you click "B" and it wraps the selection text in bold tags?

    First problem is getting it so say that nothing is selected.

    Thanks,
    e39m5

  6. #6
    SitePoint Evangelist
    Join Date
    Jun 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is my newest code, Im still not getting any alerts. Will my if statements work to find out the browser?

    Code:
    <script language="JavaScript" type="text/javascript">
    <!--
    function checkSelection() {
    	//IE
    	var field = document.post.testfield;
    	if (document.all) {
    		//Text is Selected
    		if (strSelection!="") {
    			alert ("IE: Text is Selected")
    		}
    		//Text is not Selected
    		else {
    			alert ("IE: Text is not Selected")
    		}
    	}
    	//FF
    	else if (document.getElementById) {
    		//Text is Selected
    		if(field.selectionStart - field.selectionEnd != 0){
    			alert ("Firefox: Text is Selected")
    		}
    		//Text is not Selected
    		else {
    			alert ("Firefox: Text is not Selected")
    		}
    	}
    }
    //-->
    </script>
    <form action="posting.php" method="post" name="post">
    <textarea name="testfield">
    Some sample teext for testing.
    </textarea>
    <input type="button" value="Test" onclick="checkSelection();">
    </form>
    e39m5

  7. #7
    SitePoint Evangelist
    Join Date
    Jun 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just tested in IE with this code, completely got rid of the browser check but still no alerts are coming up.

    Code:
    <script language="JavaScript" type="text/javascript">
    <!--
    function checkSelection() {
    	//IE
    	var field = document.post.testfield;
    		//Text is Selected
    		if (strSelection!="") {
    			alert ("IE: Text is Selected")
    		}
    		//Text is not Selected
    		else {
    			alert ("IE: Text is not Selected")
    		}
    }
    //-->
    </script>
    <form action="posting.php" method="post" name="post">
    <textarea name="testfield">
    Some sample teext for testing.
    </textarea>
    <input type="button" value="Test" onclick="checkSelection();">
    </form>
    e39m5

  8. #8
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,014
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)

    alert

    The code as you posted tests for the value of the variable strSelection but it has been assigned any value. Try something like this.
    HTML Code:
    <html><head>
    <script language="JavaScript" type="text/javascript">
    <!--
    function checkSelection() {
    var strSelection = document.getElementById('testfield').value;
    alert(strSelection);
    	//IE
    	var field = document.post.testfield;
    		//Text is Selected
    		if (strSelection!="") {
    			alert ("IE: Text is Selected")
    		}
    		//Text is not Selected
    		else {
    			alert ("IE: Text is not Selected")
    		}
    }
    //-->
    </script>
    </head><body>
    <form action="" method="post" name="post" id="post">
    <textarea name="testfield" id="testfield">
    Some sample text for testing.
    </textarea>
    <input type="button" value="Test" onclick="checkSelection();">
    </form>
    </body></html>


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
  •