Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Can anyone help me to get onclick to work?

    Can anyone help me to get onclick to work? I keep getting a mouse over instead. Do you know what I'm doing wrong? I checked the error console but I have no idea what to do with the information. Here's the code I'm using:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>onclick Demo</title>
    </head>
    <body>
    <img src="Picture 322.png" 
    onclick="this.title='New Title';" /> 
    </body
    </html>
    Thanks

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,764
    Thanks
    55
    Thanked 518 Times in 515 Posts
    "this" refers to the element that is receiving the event, in this case the img

    so your code is doing exactly what it should.

    are you trying to change the title of the document?

  • #3
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I want "New Title" to pop up when I click on the image but instead "New Title" pops up when I roll over the image and nothing happens when I click.

  • #4
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    No, I'm not trying to change the title. "New Title" is the title for right now.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Yes? Again, it's doing what you told it to do. It is *CHANGING* the title of the image. Which is, indeed, the title that shows up when you mouse over the image.

    What do you mean by "want [the title] to pop up"?

    You could just do
    Code:
    <img src="Picture 322.png" onclick="alert('New Title');" />
    but alert is very much obsolete nowadays. Some browsers allow the user to block alerts, so they are *NOT* a good way of giving messages.

    So what kind of (other) pop up are you interested in?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by turtleflame View Post
    No, I'm not trying to change the title. "New Title" is the title for right now.
    No, it is not. At least not until you click on the image. Then it is.

    As your code is now, if you roll over the image *BEFORE* you click on it, you will see nothing. No title.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,764
    Thanks
    55
    Thanked 518 Times in 515 Posts
    that's exactly the behaviour of the tooltip that comes as standard with the "title" attribute.

    if you want something like you are talking about I think you will have to build it yourself

  • #8
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I just want it to do the same thing it does when I roll over the image with my mouse I just don't want the title to show unless I clicked on the image. I'm new so maybe I'm misunderstanding onclick. I don't know.

  • #9
    Regular Coder Nile's Avatar
    Join Date
    Jun 2008
    Posts
    280
    Thanks
    2
    Thanked 46 Times in 46 Posts
    Indeed, you've misunderstood onclick. Search google for "javascript tooltips" and you'll get what you wanted.

  • #10
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Okay thank you.

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    I disagree. I think he understands oncliick. He just doesn't understand what HTML ALONE does.

    He doesn't seem to be aware that rolling over an image *WILL* produce the title display.

    As I noted, he could use alert( ) for this except that it's so obsolete. But for his purposes, that may be perfectly okay.

    Creating a full blown tooltip is a lot more work. Though there are ways to make it simpler.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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