0

I am developing a discussion panel in asp.net in which I am using the jquery selector option with two classes

 $("#plblDisAgreeProblem", "plblDisAgreeComment").click(function(){
var str = {
            problemID: $("#problemID").val(),
            empID : $("#empID").val(),
            commentID : -1
        }
        $.ajax({

            type: "GET",
            url: "<%= Url.Action("GetStatus", "Discussion") %>",
            data: str,
            error: function(msg){
                    alert("error2" + msg);
                },
                success: function (msg) {

                  var out = msg.split("<br/>");
                  if (out[1] == "DisAgree: True")
                  {
                        alert("You are already disagree to this problem.");
                  }
                  else
                  {




                }
            });


        })

I want if the class of div is 'plblDisAgreeProblem' then in JSon commentID should be -1 and if the class of div is 'plblDisAgreeComment' then in JSon commentID should be this.id but how I can do this?

Please Help me

Billz
  • 1,067
  • 6
  • 25
  • 57

4 Answers4

1

Use this is() function:

commentID : $(this).is(".plblDisAgreeProblem") ? -1 : this.id

I believe this should work (can't test from where I am right now).

Becuzz
  • 6,846
  • 26
  • 39
  • this should work, although I would use the "comment" class and swap the order of the -1 and this.id - but that is just a preference on my part – Mark Schultheiss Apr 16 '12 at 18:57
1

First lets fix the missing dot in your class selector.. see below,

$("#plblDisAgreeProblem", ".plblDisAgreeComment").click(function(){

Then you can use hasClass function to check if the clicked element is from the specified class.. see below,

    var str = {
        problemID: $("#problemID").val(),
        empID : $("#empID").val(),
        commentID : -1
    }

   if ($(this).hasClass('plblDisAgreeComment') {
       str.commentID = this.id;
   } 

We don't need an else if of ('plblDisAgreeProblem') because it is defaulted to -1.

Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134
0
$("#plblDisAgreeProblem", ".plblDisAgreeComment").click(function(e) {
  ....
  ....

  if(this.className == 'plblDisAgreeComment') {
     str.commentID = this.id;
  } else {
   // do other
  }
  ......
  ......
});

missing dot(.) for class selector

thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
0

This should do the trick:(fixed a couple of syntax errors as well)

$("#plblDisAgreeProblem", ".plblDisAgreeComment").click(function() {
    var str = {
        problemID: $("#problemID").val(),
        empID: $("#empID").val(),
        commentID: $(this).hasClass('plblDisAgreeComment') ? this.id : -1
    };
    $.ajax({
        type: "GET",
        url: "<%= Url.Action("GetStatus", "Discussion") %>",
        data: str,
        error: function(msg) {
            alert("error2" + msg);
        },
        success: function(msg) {
            var out = msg.split("<br/>");
            if (out[1] == "DisAgree: True") {
                alert("You are already disagree to this problem.");
            } else {}
        });
    });
});

In reference for speed:

jQuery .hasClass() vs .is()

Community
  • 1
  • 1
Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100