3

I'm trying to load a widget in javascript which creates a HTML table once it has been loaded. Example:

<td class="foo"><a class="bar" href="http://example.com">Example</a></td>

Inside the table are links where I'd like to remove the href attribute. So the result looks like this

<td class="foo"><a class="bar">Example</a></td>

I tried the following jQuery:

function myfunction() {
        $("td a").each(function(){
            if($(this).hasClass("bar")){
                $(this).removeAttr("href");
            }
        });
    };

Reference: http://www.tutorialrepublic.com/faq/how-to-remove-clickable-behavior-from-a-disabled-link-using-jquery.php

I tried it also with onload="myFunction()" but that neither worked out.

Why this is not working? Any input is much appreciated!

2 Answers 2

3

have you try:

$(window).load(function(){
    myFunction();
})
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for your quick quality response!
2

The problem, you're facing is, contents are added after the full document load. You should use .on() method to call your function or to run the script.

$(".widget").on("load", function(){
    alert("It was loaded/ load your function/write your code here.");
});

Note, .widget and load are my assumptions. You should better use the correct values or provide your jsfiddle or code here to diagnose the problem if it still exists.

6 Comments

Thanks also for your response. I didn't get it to work with on("load") but the first comment already helped me fix the issue :)
Okay but whenever you get new contents loaded into document via ajax or something other than full page load, you will have to use .on() or .live() method. Even this .on method was okay if you replace the $(".widget") to $(window) :)
Here is a Codepen of the final result, it works nicely: Link to Codepen But of course I'd love to see the on("load") example working, so feel free to fork the Codepen :)
I have forked it. Even I have removed the anchor tag. But if its necessary, please let me know I will fork another one which will not remove that.
Thanks - but where is the link to the fork?
|

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.