0

I'm using jQuery on a JSF application. I have a radiobutton, which has some Ajax rendering when clicked and on success, I'm trying to remove it (for simplicity)

My ajax callback looks as such:

function ajaxOnSuccess(data) {
    var getId = data.source.id;
    $(getId).remove(); // This won't work 
}

console.log($(getId)) is returning [prevObject: n.fn.init[1], context: document, selector: "coverage:j_idt483:0:j_idt487:0:printableNts:select-one-radio:0"] - so I'm thinking it is not able to find the element?

How can I select the radiobutton?

zer0
  • 4,657
  • 7
  • 28
  • 49

1 Answers1

1

first of all, $(getId).remove(); is a wrong jquery selection, it should be:

$("#"+getId).remove();

but are you sure, that the id of that element realy exists? i ask you this because JSF adds prefixes to nested components id. open the sourceCode of your generated Page and look up for that id, and check if its exists exactly as you use in JQuery-Selector.

another Option could be using class selector, something like:

$(".classNameOfThatElement").remove();

or

YourElementParent.find(".classNameOfThatElement).remove();

UPDATE:

you said in your comment, that you get

"Uncaught Error: Syntax error, unrecognized expression: unsupported pseudo: j_idt483"

the colon(:) inside the generated component id causes this in JQuery byId selector.

now try this:

$(document.getElementById(getId)).remove();

this should work. i tested it :)

Rami.Q
  • 2,486
  • 2
  • 19
  • 30
  • Tried doing `$("#"+getId).remove();` but I get an error: `Uncaught Error: Syntax error, unrecognized expression: unsupported pseudo: j_idt483`. The radiobuttons are in a `ui:repeat` so i need the id of the element through which the ajax has been triggered. Also, yes, the id I get in the console log matches the id of the element in DOM – zer0 Feb 20 '16 at 01:53
  • @ultimatecoder, add the attribute prependId="false" to that form, so you can access your id AS is without prefixes, and give it a try, one more thing, alert(getId) and tell me what you get! – Rami.Q Feb 20 '16 at 02:00
  • Did that but no luck. I get `j_idt483:0:j_idt487:0:printableNts:select-one-radio:0` when I do `alert(getId)` – zer0 Feb 20 '16 at 02:09
  • @ultimatecoder, i updated my answer, give it one more try! – Rami.Q Feb 20 '16 at 02:27
  • Yes, it works!!! Thank you :) – zer0 Feb 20 '16 at 02:31