Cristian Boariu
Cristian Boariu

Reputation: 9621

what's wrong with this jQuery method?

I have this method:

function replaceRightClickIcefacesMethod() {
    var oldName = jQuery(".singlePaneOfGlassBlock").attr("oncontextmenu");
    oldName = oldName.replace('Ice.Menu.contextMenuPopup', 'contextMenuPopupUpdated');

    jQuery(".singlePaneOfGlassBlock").attr("oncontextmenu", oldName);
}

I do not understand why Firebug reports:

oldName.replace is not a function

Do you see any issue? For me it's just weird...

UPDATE: Just notice that oldName returns a function, if I do alert(oldName):

function oncontextmenu(event) {
Ice.Menu.contextMenuPopup(event, "j_id88:sectionContextMenu_sub", "j_id88:j_id111:0:j_id123:0:j_id124");
return false;

}

Upvotes: 2

Views: 132

Answers (3)

Felix Kling
Felix Kling

Reputation: 816452

Before jQuery 1.6, jQuery(".singlePaneOfGlassBlock").attr("oncontextmenu") returns a function as it returns the property of the DOM element and not the attribute (DEMO).

This is fixed in jQuery 1.6 (DEMO).

If you cannot use jQuery 1.6, you have to call getAttribute on the DOM element:

var oldName = jQuery(".singlePaneOfGlassBlock")[0].getAttribute('onclick');

Upvotes: 2

Alex
Alex

Reputation: 34978

Check if oldName is null...

Upvotes: 1

Marcin
Marcin

Reputation: 1615

Maybe remove assignation in third line? Replace may return number of entries replaced.

function replaceRightClickIcefacesMethod(){
  var oldName = jQuery(".singlePaneOfGlassBlock").attr("oncontextmenu");
  oldName.replace('Ice.Menu.contextMenuPopup','contextMenuPopupUpdated');

  jQuery(".singlePaneOfGlassBlock").attr("oncontextmenu",oldName);

}

Upvotes: 0

Related Questions