Log in

View Full Version : Script for auto select all when clicked



gaijingrl
10-25-2007, 05:09 AM
Hi everyone,

I'm a bit of a boon so I'm not sure how to technically describe this.

I have a myspace graphics website. Under each graphic is a text area form with code for that specific picture. Right-click is disabled.

How can I make it so that code is automatically selected as soon as a user clicks one time in that form? I tried searching for it on this website but I couldn't find what I was looking for. Not knowing what this is called probably doesn't help. ;)

Thanks in advance for your help!

djr33
10-25-2007, 05:36 AM
document.forms[N].elements[n].select()

Where N is the number of the form and n is the number of the element in the form.

Use this as the onClick value of the link tag, etc.

gaijingrl
10-25-2007, 07:20 AM
When I said boon I guess I was wrong and am really a noob because that was pretty much over my head. I know some html and some basic Perl and that's about it. I get the onclick thing - just that one word alone will help me hugely. I'm using Yahoo Site Builder at the moment and while it is WYSIWYG I can alter the code. I'll see if they have an onclick sort of function.

Thank you.

djr33
10-25-2007, 08:14 AM
<a href="#" onclick="dostuff();">text</a>

An a tag is an anchor, usually used for links.
"text" is what is displayed and formatted as a link; </a> just closes the tag.
(That much you should already know.)

href gives it an address and makes it behave as a link. Using # is equivalent to no change, so the javascript can act instead of going to a new page.

onclick will perform a javascript action when it is clicked. same for onmouseover, onload, etc.

dostuff(); is a very simple javascript function, which might do anything, depending how it is defined.

In this case,
onClick="document.forms[N].elements[n].select();"

That would select the nth element of the Nth form.

On a page, each element has some rank on the page. You'll have to figure out which one is the right field.

OR, you could get the field another way. Once you have it, just use thatmethod.select();

Some ideas would be getElementByID(), or document.forms['name'].elements['name'], etc.

Also, just a warning-- myspace blocks some scripting so you may run into some errors just because of that.


One more note on using javascript in links:
By adding return false to the end of the javascript statement, it will make the href not fire, so you could have a backup href if the javascript was disabled:
<a href="nojavascript.htm" onclick="dostuff(); return false;">
So it would go to that page only if javascript didn't stop it.

Or you could stop the annoying # from appearing in the address bar (doesn't change the url of the page, just what is displayed in the address bar), like:
<a href="#" onclick="dostuff(); return false;">

jscheuer1
10-25-2007, 08:36 AM
Just don't disable right click.

Disabling right click protects nothing.

Even if you can select the text via javascript, that doesn't get you anything. You still cannot copy the selected text as conveniently without right click as you could with right click.

Even with right click disabled, you can select the text with left click + drag and copy it with Ctrl + C.

Images can be gotten in a myriad of ways as well. Disabling right click (which doesn't work in all situations anyway - it depends upon the browser and its settings) only takes away the easiest method.

If something is on the web, it can be copied.

djr33
10-25-2007, 09:36 AM
Correct. Images can't be protected, no right click is annoying, etc etc.

The easiest way to copy all the text is just to triple click in the text field and hit ctrl+c, I'd say, though that's based on the user, not the code of the page. Or use ctrl+a, which is much easier than right click + select all, anyway.