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

  • SEARCH
  • Home
  • 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 8458835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:11:10+00:00 2026-06-10T13:11:10+00:00

I have to dynamically create series of links that when clicked – change some

  • 0

I have to dynamically create series of links that when clicked – change some others elements properties (src, class, text), basing on some data taken from elsewhere (json).

I’ve got such a piece of code:

  // this is data I need processed
  screenshots = report_data.screenshots;
  // for each I'd like to create a link
  for (var i=0; i < screenshots.length; i++) {
      document.write('<a href="" onclick="{document.getElementById(\'the_screenshot\').src = \''+ screenshots[i].screenshotFile + '\'; ' + 'return false;}">' + screenshots[i].screenshotTitle + '</a>');
  });

However proper escaping all those characters took me two hours.
I’d like to place there a call to elsewhere defined parametrized function.
Then I’d only have to input the parameters.

I tried of many various combinations of what I’ve found on stackoverflow.com
leading me to a terrible headache.

Please, help.

  • 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-06-10T13:11:11+00:00Added an answer on June 10, 2026 at 1:11 pm

    Accidentally, after hours of trying and retrying I asked my friend and his wise head told me to do it somewhat more sophisticated way:

    • Create a parametrized function in plain javascript.
    • Use closures – assign to onclick only the anonymous function that calls the above mentioned with adequate parameter.
    • Create the pattern string of the tag to pass to the document.write(html) with variable-like section for onclick function code.
    • Use string.replace(A,toB) function: create a string with function code to assign.
    • Do the same trick with the function parameter if required.
    • Pass the filled in pattern to document.writa(html) function.

    The final solution looks like this (may be not exactly same, but the idea is pointed):

    // function to call by closure
    onLinkClick = function(SRC) {
        document.getElementById('the_screenshot').src = SRC;
        // and here one can place as much code as he needs // plain js
        // and I did it
    }
    
    // this is data I need processed
    screenshots = report_data.screenshots;
    for (var i=0; i < screenshots.length; i++) {
        // first enlist every basic part with it's inner parameters as text
        var pattern = '<a href="" onclick=ONCLICK>TEXT</a>'
        var onclick = '(function (){onLinkClick(PARAM); return false;})()';
        var text = screenshots[i].screenshotTitle;
        var param = screenshots[i].screenshotFile;
        // now inject them - without any more escaping via replace
        onclick = onclick.replace('PARAM', param);
        pattern = pattern.replace('ONCLICK', onclick);
        pattern = pattern.replace('TEXT', text);
        document.write(pattern);
    });
    

    This way the code looks much more elegant than on the first approach.
    I hope this helps those, who will have to face javascript in future.

    One more thing, I used to think a detail, but it came out to be a really serious: syntax. To make a closure function do it’s work:

    • write a function just like usual
    • surround it with parentheses
    • follow it by a another pair of empty parentheses

    like this:

    (function(){return false;})()
    

    Now it’s an anonymous function, not just an anonymous function call. And now it works.

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

Sidebar

Related Questions

I might have to make some Java classes that dynamically create strings with HTML
I have been curious about dynamically create class at runtime in C# and stumbled
I have a script that allows me to dynamically create lists via jQuery. I'd
I'm using Apache BCEL to dynamically create java classes that will each have its
I have a noob-question, that might be similar to Dynamically create multiple instances of
I have a python script that is trying to create a directory tree dynamically
I have a HTML form where i dynamically create elements and set its name
I have a series of dynamically created GridViews that I load. Everything works fine
Let say that for each customer you have to dynamically create a database when
I have a several form elements that I am dynamically creating with javascript when

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.