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 6584793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:34:52+00:00 2026-05-25T16:34:52+00:00

function on_click(e) { if( !e ) e = window.event; dragok = false; document.onmousemove =

  • 0
function on_click(e)
{
  if( !e ) e = window.event;
   dragok = false;
   document.onmousemove = null;
   var x = e.target||e.srcElement;
  if( dx > 200 ) // dx is the numbers of pixels from leftside of mouse pointer
    {
      // document.write(dx); this is working it prints dx value but unable to create an image        
      var img = IEWIN ? new Image() : document.createElement('img');
      img.src="cool.jpg";
      document.getElementById(x.id).appendChild(img);
    }
 }

I found these snippet on Stack Overflow but not working when I tried it . Here it is What is the best JavaScript code to create an img element.

The aim is to create an image when function on_click is called, (function on_click is called for onclick event). dx is the pixels measured from the left side. So if the Mouse pointer is greater than 200 pixels and if the mouse is clicked, It should create an image at that place I mean if dx is 206 then an image should be created with the distance of 206 pixels, but I’m unable to make create an image element with JavaScript.

What changes I have to make in JavaScript code?

  • 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-25T16:34:53+00:00Added an answer on May 25, 2026 at 4:34 pm

    All you need is this for all browsers:

    var img = new Image();
    img.src = "cool.jpg";
    img.className = "myClass";
    img.id = "mainImage";
    x.appendChild(img);
    

    If you already have the DOM element, there’s no reason to get the id and then do document.getElementById(x.id). Just use x directly.

    To use CSS rules that refer to this image, you would make rules like this (depending upon whether you’re referring to the class or the id):

    .myClass {border: 1px solid #F00;}
    #mainImage {padding: 14px;}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

$(div#foobar).live('click', function() { }); $(window).hashchange(function() { }); How can I stop firing hashchange event
dojo.addOnLoad( function() { attach on click to id=textDiv dojo.query('#Specific_consultant-Yes').onclick( function(evt) { // document.getElementById(Consultants).style.visibility =
On the click of a button this Javascript is called: var xmlhttp; function register()
i have many events such as $('.hidefile').click(function (event) { On page load and on
On document ready I run this code: jQuery(document).ready(function(){ jQuery('#button').click(function() { jQuery('#contact_form').load(/Users/mge/Downloads/jquery-ajax-1/readme.txt); return false; });
i have structure like this <table><tr><td></td><td><input type=button name=button></td></tr></table> <script> $('tr').click(function(){window.alert('tr');}) $(':input').click(function(){window.alert('input');}) </script> when i
the jquery code is like this: $(document).ready(function() { //When page loads... $(.tab_content).hide(); //Hide all
I'm trying to open a new window like so: $('#wrapper').click(function() { window.setTimeout(function() { //alert('hi');
I am generating events on fullCalendar with this code <script type=text/javascript> $(document).ready(function() { $('#calendar').fullCalendar({
I like to scope all of my JQuery functions and event sinks to $(window).load,

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.