• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 832
  • Last Modified:

disabling text select for text but not form elements

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
iaindooley
Asked:
iaindooley
1 Solution
 
gphCommented:
el = event.srcElement

if (el.type=='text' || el.type=='textarea') {
return
}
0
 
gphCommented:
I think event.srcElement is IE only.  There may be a Netscape equivalent.  I just don’t have a need for it.
0
 
gator4lifeCommented:
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
 
MatrixDudCommented:
- 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
 
iaindooleyAuthor Commented:
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

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now