0

I have an ajax function which has a success block. In this block I'd like to remove an element.

the success block contains the following :

success:function(data, textStatus, jqXHR) {

$("#comment +$data['post_id']").remove();
},

I got an error Syntax Error : unrecognized expression #comment +$data['post_id']

Where is the mistake ? how can I use the post_id contained in data ?

Thank in advance for your help

klark
  • 484
  • 1
  • 10
  • 27

1 Answers1

1

JavaScript variables are not referenced with a $ unless you're dealing with jQuery-

Also, the variable cannot be in quotes. Furthermore, notice the array notation in the following snippet. I think both work in JavaScript, but it is worth noting. Thus, if data['post_id'] is the HTML id of the thing you want to remove - you would do this:

success:function(data, textStatus, jqXHR) {

 $("#comment"+data.post_id).remove(); }, // data['post_id'] should also work.

so that it is selecting something like the following, after the variable is rendered:

$("#comment2").remove();

Provided data.post_id renders into a number.

rm-vanda
  • 3,122
  • 3
  • 23
  • 34
  • Java != Javascript >:-| – John Dvorak Mar 12 '14 at 17:39
  • Yeah, yeah, yeah, i was trying to answer quickly fast. – rm-vanda Mar 12 '14 at 17:40
  • Why haste? Proofreading doesn't cost more than the asker should be willing to spend. – John Dvorak Mar 12 '14 at 17:41
  • http://meta.stackexchange.com/questions/17204/six-simple-tips-to-get-stack-overflow-reputation-fast – rm-vanda Mar 12 '14 at 17:42
  • the result I'd like to get is #comment2 if 2 is the value of post_id @rm-vanda – klark Mar 12 '14 at 17:44
  • 1
    @rm-vanda check the accepted answer... SO isnt about reputation, it's about helping... – Karl-André Gagnon Mar 12 '14 at 17:45
  • 1
    @sanfisa: Then do `$('#comment' + data.post_id)`. This is basic string concatenation and foremost has nothing to do with selectors. Maybe I should make this clear: Selectors are just strings. *How* that string is generated is irrelevant since jQuery will always only see the final string. – Felix Kling Mar 12 '14 at 17:45
  • edited the answer accordingly. – rm-vanda Mar 12 '14 at 17:46
  • If you don't think the final result was a well-written answer, then down vote. It took me less than 5 minutes to make it higher quality, and the only thing you two complained about was "Java" vs "JavaScript" which is ultimately irrelevant in this context. – rm-vanda Mar 12 '14 at 17:47
  • 1
    @rm-vanda I wasn't complaining? I was arguing over the link you posted. You used a question to answer *"Why haste"* when the answer of this question is about quality. I though it was ironic. Anyway, not gonna start a debate on an other subject than the actual question. – Karl-André Gagnon Mar 12 '14 at 17:51
  • Sorry maybe I was not clear enough. Post_id is not the id I want to remove. It has a value, depending on which post has been commented. So, post_id is equal to a numeric value that I want to concatenete with #comment @rm-vanda – klark Mar 12 '14 at 17:53
  • Well, the only problem in your snippet was your quoting. You need to put the variables outside of the double-quotes. i.e. : `$("#comment #opinion"+data['post_id']).remove();` - does that make sense? – rm-vanda Mar 12 '14 at 17:55