tags:

views:

36

answers:

3

How do I set the cursor on a div-element in javascript?

I have a form(div-element not input) with a textstring attached on it. I want the textstring removed and the cursor set at the beginning of the element onclick.

I have removed the content in the div-element with onclick event with: divElement.innerHTML = '';

Now I want the cursor to be set?

A: 

If you mean the mouse pointer, use the CSS cursor style like this:

#mydiv {
    cursor: help;
}

There are a whole load of standard options you can use. You can also define a graphic to use as the pointer (though this has cross-browser compatibility issues).

See this page on Quirksmode for more info.

Similarly, if you want to do it dynamically in Javascript, then just set the object's style:

document.getElementById('mydiv').style.cursor = 'help';
Spudley
I think you missunderstand my question. I have a form(div-element not input!) that have a textstring on it. Onclick on that div-element I want the cursor to be set at the beginning of the div-element so the user can fill in the form. The textstring will be removed after onclick event
Woho87
okay, so you mean the text cursor, rather than the mouse cursor? (yes, it's confusing that the same term is used for both. If you want to distinguish them, you could use 'pointer' for the mouse and 'caret' for the text)
Spudley
I'll make another answer for setting the text cursor....
Spudley
A: 

divElement.style.cursor = 'whatever';

If you want to move the cursor to be over the divElement, then you can't.

Matt
A: 

If by 'cursor', you mean the text cursor (aka the caret), I presume what you're really asking is how to make a div into an editable content box.

What you need is to set the contentEditable attribute on your div.

If you want it editable from the start, just include it in your HTML code:

<div contentEditable="true">....</div>

If you want to switch it on/off, you can set it in javascript:

mydiv.contentEditable="true"

However, the only time I can think of when it's better to use contentEditable rather than a textarea is if you're writing a WYSIWYG HTML editor.

Most of the rest of the time I would say it's probably preferable to use a <textarea>. You can style it to look like the rest of your page, and you can make it readonly or disabled when you don't want it changed. But it is much easier to work with in a form and in Javascript. The problem with using a div is that it can contain other html tags, which may affect how it works, and will likely open you up to security problems if you make it directly editable.

Spudley
Actually, for WYSIWYG HTML editor, `designMode` would be better
Yi Jiang
I tried the text area and it worked! but I use google Chrome how do I remove the ugly marks at the downright area? The ugly marks appear in textarea tags
Woho87
Chrome and Safari allow the user to resize a textarea box. This is what those marks are in the bottom corner. You can get rid of it by setting the max-height and max-width to the same as the height and width of the box in your stylesheet; this turns off the resizing ability.
Spudley