Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Javascript select all text in field upon clicking

Explorer ,
Sep 15, 2016 Sep 15, 2016

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.

TOPICS
Acrobat SDK and JavaScript
4.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 15, 2016 Sep 15, 2016

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 15, 2016 Sep 15, 2016

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 15, 2016 Sep 15, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 15, 2016 Sep 15, 2016

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 21, 2016 Sep 21, 2016

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 21, 2016 Sep 21, 2016
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines