CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2006
    Posts
    62

    Javascript Click On Page Puts Image

    Hey, I am trying to make a script so that when you go to the page and click anywhere (even on images) it will put an image right at your mouse point. I am going to have the little image as a checkmark but how would I go about doing that? I know how to go into the script onclick but i am not sure how to put the image right were you clicked. I was thinking clickLoc() but I am unsure on how to use it and can't find any decent examples online.

    Thanks

  2. #2
    Join Date
    Feb 2005
    Location
    Denmark
    Posts
    742

    Re: Javascript Click On Page Puts Image

    Does the page have any other content then the image, because if it does, it will be tricky to do - but not impossible.

    However, basically what you need to do is to read the X and Y coordinate of the mouse-click event, and then "place" the image at that location by using the DOM tree by using the position options there, but this is where the page design and content comes into play. So I think you'll have to provide more information before people can help you like this.

    Otherwise, try starting with capturing the mouse click event and subtract the X and Y coordinates - that would be the first step.

  3. #3
    Join Date
    Apr 2006
    Posts
    62

    Re: Javascript Click On Page Puts Image

    This is going to be with a few images, there shouldn't be any text. I want to be able to click multiple times also without the original disappearing or anything. Thanks

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: Javascript Click On Page Puts Image

    This is just a simple combination of two parts.
    • Get mouse coordinates
    • Create image element with those coordinates


    Get mouse coordinates
    Code:
    if (document.all) {
      xpos = event.clientX + document.body.scrollLeft;
      ypos = event.clientY + document.body.scrollTop;
    }
    else {
      xpos = e.pageX;
      ypos = e.pageY;
    }
    Create image element with those coordinates
    Code:
    var theBody = document.getElementsByTagName('body').item(0);
    
    var theImage = document.createElement('img');
    theImage.src = 'path/to/check.png';
    theImage.style.position = 'absolute';
    theImage.style.left = xpos + 'px';
    theImage.style.top = ypos + 'px';
    theBody.appendChild(theImage);
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Apr 2006
    Posts
    62

    Re: Javascript Click On Page Puts Image

    Thanks, works great in IE, I have been trying EVERYTHING and can't get it working in FF. I googled until my eyes bled and it seems to be a very common problem but I can't find a fix that works for this.

  6. #6
    Join Date
    May 2002
    Posts
    10,943

    Re: Javascript Click On Page Puts Image

    There should be no reason why if won't work in Firefox. What does your Error Console say? Did you think of debugging?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  7. #7
    Join Date
    Apr 2006
    Posts
    62

    Re: Javascript Click On Page Puts Image

    I didn't get any errors (because I don't know where they would show) but I tested a few things. I tried an alert in the if and else statements and it comes up in the else. Then I put the alert BELOW,

    xpos = e.pageX;
    ypos = e.pageY;

    and it did not come up. I guess the error is somewhere there then but I can't figure out how to fix it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured