?
Solved

disabling text select for text but not form elements

Posted on 2003-03-11
5
Medium Priority
?
830 Views
Last Modified: 2011-04-14
hi, i wanted to have one function that i can use during onmousedown or onstartselect that will check if the user has clicked on a form element because its really annoying not being able to select the text inside an input. i found document.activeElement, but it would appear that only works for IE :-(

does anyone know an easy way of doing this? the only other way i could think is by using the onfocus and onblur events of EVERY form element but that is a bit tedious...
0
Comment
Question by:iaindooley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 3

Expert Comment

by:gph
ID: 8115396
el = event.srcElement

if (el.type=='text' || el.type=='textarea') {
return
}
0
 
LVL 3

Expert Comment

by:gph
ID: 8115411
I think event.srcElement is IE only.  There may be a Netscape equivalent.  I just don’t have a need for it.
0
 
LVL 5

Accepted Solution

by:
gator4life earned 225 total points
ID: 8116796
Actually, the event mentioned in the question is supposed to be onselectstart.

And, it really isn't that hard to add onfocus and onblur events to all of the form elements; just use DHTML to do it when the page loads:


function AddEvents() {

     var aInputs = document.getElementsByTagName("INPUT");
     var aTextareas = document.getElementsByTagName("TEXTAREA");

     for (var i = 0; i < aInputs.length; i++) {

          if (aInputs[i].type == 'text') {

               aInputs[i].onfocus = [something 1];
               aInputs[i].onblur = [something 2];
          }
     }

     for (var j = 0; j < aTextareas.length; j++) {

          aTextareas[j].onfocus = [something 1];
          aTextareas[j].onblur = [something 2];
     }
}

<body onload="AddEvents()">

gator4life
(chomp, chomp)
0
 
LVL 1

Expert Comment

by:MatrixDud
ID: 8116966
- Simply put a variable in your <head>:

var myState = false;


- Then your <body> use this:

onSelectStart="return myState"


- Then in your input or textarea use this:

onMousedown="myState = true" onMouseUp="myState = false"


This will disable selections in all but but you specify the mouse events in.
0
 

Author Comment

by:iaindooley
ID: 8117950
okay, that's cool... getElementsByTagName is only IE6 though. i suppose i could use document.forms arrays to run through it.

cheers

iain
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question