0

I am not a gold star medal with Javascript, so please bear with me.

We have an addition on the site that is being loaded externally via a a javascript (its called ratecard), however the link that is supplied that the item is being used for isnt correct, so with a little javascript/jquery we are trying to overwrite it.

As the plugin is loaded it adds to a predefined div a location.href. I am trying to overwrite this via following javascript.

This is the div that is being generated

<div class="wdgt_widget" style="border:1px solid #1d8be0 !important; border-radius:8px  !important; padding:11px  !important; width:300px !important; height:64px  !important; cursor:pointer  !important; box-sizing: border-box  !important;" onclick="location.href='https://ratecard.io/staffing-ms'"></div>

So I am trying to unbind the click action first and add a new one, yet nothing happens. See script below.

        <script>
            jQuery(document).ready(function ($) {
                setTimeout(function() {
                    $('#wdgt_widget').unbind();
                    $('#wdgt_widget').attr('click' 'location.href="https://ratecard.io/staffing-ms/review/");
                }, 5000);   
            });
        </script>

I added a 5 second timer on it, since i wanted to make sure the code would be loaded and div already be generated.

Sadly nothing happens. Is there a possibility to simply use replace on the onclick? or did i do something wrong here?

3
  • You should be using .wdgt_widget instead of #wdgt_widget. I Suppose. Since your div doesn't have a id. Commented Nov 30, 2016 at 12:49
  • and the attribute name is onclick not click Commented Nov 30, 2016 at 12:49
  • And unbind() doesn't unbind inline script. And you have synthax error in your posted code... Commented Nov 30, 2016 at 12:50

4 Answers 4

1

Problem: 1) This $('#wdgt_widget') You are using # in the selector which is used to select by id. But you have a class in your div so use .

Solution: 1) Replcae the above mentioned syntax with this $('.wdgt_widget')


Problem : 2) You have a missing ' on the end of the attr function and also a , between your click and location.

$('#wdgt_widget').attr('click' 'location.href="https://ratecard.io/staffing-ms/review/"); // , and ' missing.

Solution: 2) change .attr('click' 'location.href="https://ratecard.io/staffing-ms/review/") to .attr('click','location.href="https://ratecard.io/staffing-ms/review/"') Note I added a , in between and ' at the end.

Sign up to request clarification or add additional context in comments.

2 Comments

Exactly what I was looking for. I realized as I was posting this already my class and id mixup ... but the command and quote at the end did it for me. Awesome
@Dorvalla glad I could help.
1

You should do this:

$(document).ready(function () {
    setTimeout(function() {
        $('.wdgt_widget')
          .unbind()
          .attr('onclick', 'location.href="https://ratecard.io/staffing-ms/review/"');
    }, 5000);   
});

Comments

0

You should be using .wdgt_widget instead of #wdgt_widget. Since your div doesn't have a id.

<div class="wdgt_widget" style="border:1px solid #1d8be0 !important; border-radius:8px  !important; padding:11px  !important; width:300px !important; height:64px  !important; cursor:pointer  !important; box-sizing: border-box  !important;" onclick="location.href='https://ratecard.io/staffing-ms'"></div>

<script>
            jQuery(document).ready(function ($) {
                setTimeout(function() {
                    $('.wdgt_widget').unbind();
                    $('.wdgt_widget').attr('click' 'location.href="https://ratecard.io/staffing-ms/review/");
                }, 5000);   
            });
        </script>

Comments

0
$(document).ready(function () {
  setTimeout(function() {
     $('.wdgt_widget').unbind().attr('onclick' 'location.href="https://ratecard.io/staffing-ms/review/");
  }, 5000);   
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.