• TabyP

    (@tabyp)


    Hello,

    I’ve been trying everything in order to add a simple onclick event here and there in some pages and posts. What I need to add is this:

    <a style="cursor:pointer;" onclick="javascript: alert('Various text according to situation');">*</a>

    If you check the home page or the posts in my website, you will see asterisks with disclaimers which need to be there by law. Of course everytime I edit the page, the code gets stripped. What can I use instead of that?

    Thanks

    http://www.phylnex.com/

Viewing 15 replies - 1 through 15 (of 24 total)
  • maya923

    (@maya923)

    Ironically, I came here for the exact same question, except it’s to make PayPal buttons.

    My code is as follows:

    <input onclick="x=window.open('https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=XXX')" type="button" value="Add to Cart" />

    It always worked until I upgraded to 3.9.

    Help?

    Thread Starter TabyP

    (@tabyp)

    Yeah that’s what happened to me. It worked before without any problem. Then I updated to 3.9…

    I hope someone replies soon

    meraj.khattak

    (@merajkhattak)

    Does this problem get solved? I am able to click on the * and it opens an alert window showing the disclaimer message.

    I am using Chrome latest version.

    Thread Starter TabyP

    (@tabyp)

    Some pages still work because I haven’t edited them since the wordpress update. Check here http://www.phylnex.com/let-me-tell-you-my-story/
    You can see the asterisk and if you check the element with dev tools, the onclick is stripped. That is because I have edited the page AFTER the WordPress update. I’m still waiting for someone to post a solution.

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Why are you using inline JS, TabyP? I’m genuinely interested.

    Thread Starter TabyP

    (@tabyp)

    I’m not sure I know any other method to be honest? I need different types of popups in different locations of a page. I thought that would be the easiest and cleaner way? It was working afterall… If you have any other idea please… I’m all ears…

    meraj.khattak

    (@merajkhattak)

    I have my blog setup with latest WordPress and theme Twenty Thirteen. I added this code in text mode in a post on my blog:

    <a style="cursor:pointer;" onclick="javascript: alert('Various text according to situation');">*</a>

    and it seems to be working correctly.

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    You can use a plugin to hold your JS, e.g. this one http://wordpress.org/plugins/zia3-css-js/

    E.g. to replicate this:

    <a style="cursor:pointer;" onclick="javascript: alert('Various text according to situation');">*</a>

    You’d do this:

    jQuery(document).ready(function($) {
    
     $('a').click(function() {
      alert('Various text according to situation');
     });
    
    });

    Let us know if you need more help with the JS

    Thread Starter TabyP

    (@tabyp)

    I will try the plugin and code and let you know. Thanks!

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    The plugin does say you can use inline JS so there may be a way to use your original technique

    Thread Starter TabyP

    (@tabyp)

    Actually, I don’t understand. As you can see I use an asterisk in my code. I need to use that so that people will know that they have to click there. I don’t see where I should put the asterisk in your code.

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Oh

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    In your code you’d have your link still:

    <a style="cursor:pointer;" href="#">*</a>

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Give it an ID actually

    <a style="cursor:pointer;" href="#" id="that-link">*</a>

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Then you use the JS to target that link:

    jQuery(document).ready(function($) {
    
     $('#that-link').click(function() {
      alert('Various text according to situation');
     });
    
    });

Viewing 15 replies - 1 through 15 (of 24 total)
  • The topic ‘Javascript in WordPress pages’ is closed to new replies.