Explore the community Forums Lectora Lectora Questions & Answers JavaScript: Getting Object Names

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #283381 Score: 0
    Profile photo of appdevpub
    appdevpub
    Member
    beginner
    intermediate
    friend finder
    @appdevpub

    Hi,

    I am pretty new to Publisher but I have two quick yet important questions:

    1. Can I use JavaScript, attached to a Page, to get all the names of objects on a page? All the text fields, etc. Maybe there’s a way to use the DOM to loop through a Page object?

    2. Also, I tried using Run JavaScript on Show event of a Page to run
    document.getElementByID(‘HTML_Object_Name’).write(‘hello, world’);

    Is this the correct approach to accessing Lectora objects in html?

    Thanks.

    #287078 Score: 1
    Profile photo of Ben Pitman
    Ben Pitman
    Member
    4 pts
    @benpitman

    Yes you can. I have done it a lot. The code is NOT easy. If you send me an email, I will be glad to reply with the code.

    Complex JavaScript in Run JavaScipt action is tricky. You need to use a lot of { } even when there is only one stmt after an IF, especially if there is an Else. Best to put Functions in HTML obj or an attached JS file.

    This post has received 1 vote up.
    #299889 Score: 0
    Profile photo of Rob Ball
    Rob Ball
    Member
    @rball3500

    Ben, I am sending you a PM, I hope you are still watching this forum,

    #299905 Score: 1
    Profile photo of Darrel Somoza
    Darrel Somoza
    Member
    contributor
    friend finder
    beginner
    intermediate
    junior moderator
    advanced
    wise owl
    curious george
    profile
    209 pts
    @Klaatu

    Putting this script (found on stack overflow – http://stackoverflow.com/questions/7115022/how-do-i-enumerate-all-of-the-html-ids-in-a-document-with-javascript) to run on a button click will provide an alert with all the element ID names on a page. You can probably use this as a starting point and customize it to show only what you are looking for.

     

    var allElements = document.getElementsByTagName(“*”);
    var allIds = [];
    for (var i = 0, n = allElements.length; i < n; ++i) {
    var el = allElements[i];
    if (el.id) { allIds.push(el.id); }
    };
    alert(allIds);

     

    Darrel

    This post has received 1 vote up.
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.