Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5998581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:25:36+00:00 2026-05-23T00:25:36+00:00

I have a html component that includes some javascript. The component is a file

  • 0

I have a html component that includes some javascript.

The component is a file in a template engine, so it can be used

  • in the initial rendering of the whole html page
  • as stand-alone html rendered through an ajax request

The javascript should be applied to an object in the template, i.e. :

<div class="grid" >
  <div class="item" id="item_13">
      This is item 13
  </div>
  <div class="item" id="item_14">
      This is item 14
  </div>
</div>
<script type="text/javascript">
    $(document).ready(function(){
       $(HOW_DO_I_GET_PREVIOUS_ELEMENT???).someEffect(params)
    })
</script>

I’ve checked this similar question but the best answers seem to rely on the current script being the last one in the ‘scripts’ variable as the next ones are not loaded yet. If I append the html and js with an ajax request, it will not be the case.

To be 100% clear : the question is about getting the previous object WITHOUT reference to any specific attribute : no unique id for the tag, no random id as there is theoretically always a chance it will show up twice, no unique class attribute,as exactly the same component could be displayed in another part of the HTML document.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:25:37+00:00Added an answer on May 23, 2026 at 12:25 am

    Simple solution involving a two step process:

    1) find out which element your script tag is

    2) find the previous sibling of that element

    in code:

    <div id="grid">
        <!-- ... -->
    </div>
    <script type="text/javascript">
        var scripts = document.getElementsByTagName("script");
        var current = scripts[scripts.length-1];
        var previousElement = current.previousSibling;
        // there may be whitespace text nodes that should be ignored
        while(previousElement!==null && previousElement.nodeType===3) {
            previousElement = previousElement.previousSibling; }
        if(previousElement!==null) {
            // previousElement is <div id="grid"> in this case
            $(document).ready(function(){
                $(previousElement).someEffect(params);
            });
        }
    </script>
    

    Is this good web programming? No. You should know which elements should have effects applied to them based on what you’re generating. If you have a div with an id, that id is unique, and your generator can tell that if it generates that div, it will also have to generate the js that sets up the jQuery effect for it.

    But let’s ignore that; does it work? Like a charm.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building an app with Adobe AIR that contains a browsing component with mx:HTML
I'm using a third party component that I call a method passing a model
I have a simplified test scenario useful for asking this question: A Product can
I'm using CakePHP's ACL component to manage permissions for my app. I have about
Delphi 2010 introduced custom attributes which can be added to type declarations and methods.
I'm running into an issue with Wicket generating markup for elements that programmatically need
I want to be able to add a specified time to a javascript clock
I am using helios 3.6.2. I have an .xhtml opened with Web Page Editor
I'm looking for a way to select and upload multiple files to Amazon S3,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.