Skip to main content
Inspiring
Sep 15, 2016 5:58 PM UTC
Question

Javascript select all text in field upon clicking

Hello all,

Im using Adobe Acrobat Standard DC

I have been trying to figure this out all day and have a deadline approaching.

I have very little knowledge in writing javascript so I am rather clueless.

I am trying to figure out a script to run that allows my form boxes to highlight all the text in it upon clicking the box. Currectly where ever I click it drops my cursor, usually in the middle of a text string.

I also need this to happen for all my text fields so I'm looking for something that I do not have to input the name of the field for every single box.

After digging online I found this, but im not sure it will work for me.

<script type="text/javascript"> function SelectAll(id)

{

document.getElementById(id).focus(); document.getElementById(id).select();

}

Any help would be very much appreciated.

This topic is closed to new replies. Start a new post to keep the conversation going.

6 replies

smizyAuthor
Inspiring
Sep 21, 2016 3:24 PM UTC

I was able to find this code upon searching. I dont know enough about javascript to know if it will do what I need. Anyone able to take a look?

Here is original thread

javascript - Select text with just one click - Code Review Stack Exchange

function SelectText(element) {

    var doc = document,

        text = doc.getElementById(element),

        range,

        selection;

    if (doc.body.createTextRange) {

        range = document.body.createTextRange();

        range.moveToElementText(text);

        range.select();

    } else if (window.getSelection) {

        selection = window.getSelection();       

        range = document.createRange();

        range.selectNodeContents(text);

        selection.removeAllRanges();

        selection.addRange(range);

    }

}

$(function() {

    $('p').click(function () {

        SelectText('autoselect');

    });

});

Translate
Inspiring
Sep 21, 2016 4:11 PM UTC

That code is for an HTML form as well and won't work with a PDF form.

Translate
smizyAuthor
Inspiring
Sep 15, 2016 8:23 PM UTC

Thanks george. Yes it seems there is no way to do this. I was able to add buttons to highlight the field but now ill need 50 buttons next to every box...seems like such a common thing to not have a solution to.

Translate
Inspiring
Sep 15, 2016 6:39 PM UTC

That code is designed to work with an HTML form and won't work with a PDF form.

I think the best you can do for a field in a PDF is the following in the Mouse Enter event:

// Mouse Enter script for text field

event.target.setFocus();

Selecting all of the text is a side-effect of using the setFocus method. What I suggested results in non-standard behavior, but it might be OK for your needs.

Translate
smizyAuthor
Inspiring
Sep 15, 2016 7:26 PM UTC

Hey George,

Thanks so much for the response!

I tried it out and it would work well, however this will mostly be used on mobile devices. I am unable to hover on my mobile so this doesn't work for me. Any other ideas??

Thanks

Translate
Inspiring
Sep 15, 2016 7:50 PM UTC

There's not anything that will work with the mobile versions of Acrobat/Reader.

Translate