views:

94

answers:

3

I'm calling a javascript function in an inline onclick like so:

<a href="#" onclick="removeAttribute('foo', 'bar');">Some Link</a>

When I click on the link, though, nothing happens. I have other links (to other functions) tied to onclicks that work fine elsewhere on the same page. All links to this "removeAttribute" function fail.

There are no errors in Firebug, and the onclick event handler is being invoked - but stepping into the removeAttribute function ends up, for some reason, somewhere in jQuery.js. At no point does removeAttribute ever get called.

If I do:

javascript:removeAttribute('foo', 'bar');

in Firefox's address bar. The function is called successfully.

Anyone seen this?

+1  A: 

Try this:

<a href="javascript:removeAttribute('foo', 'bar');">Some Link</a>

It probably isn't working because of the '#' for the href value. So instead put the javascript as the href, which effectively is the same thing as putting it in the address bar.

gmcalab
Thanks! This worked. The weird thing I'd still like to figure out is - I have plenty of other links on the same page using the "#" has href and they work just fine...
potatolicious
No, this is a pretty ugly solution.
Pointy
@Pointy, agreed.
gmcalab
My suspicion is that there's something, elsewhere in the Javascript, that's whacking the "onclick" handler. If, that is, the description of the behavior is accurate.
Pointy
+4  A: 

Are you calling your own method? If so it is confusing to call it removeAttribute because it is already defined as method attached to DOM nodes. When your event handler is called its scope is defined as the Node that was clicked. Your code is probably calling the builtin method on the object the was clicked. Try using a different name or putting your method inside of a Javascript object so you can call it explicitly.

BeWarned
Ahah! This was it exactly :) Changing the function name to something else worked. Thanks :)
potatolicious
A: 

If you're using jQuery, why not leverage it? Assuming your version of removeAttribute() does the same thing as the native javascript method removeAttribute(), you could do something like:

<script type='text/javascript'>
    $(document).ready(function(){
        $('a#somelink').click(function(){
            $('#foo').removeAttr('bar');
        });
    });
</script>
munch