0

I have this code :

jQuery('#btSave').click(function (event) {
    var jqxhr = $.post("Controller/Action", {
        lastName: $("#LastName").val()      
    },
    function (data) {
        //here
    })
});

I'd like to know if in the "data" variable the id "Mydiv" exist

How can I don this ?

Thanks,

TheBoubou
  • 19,487
  • 54
  • 148
  • 236
  • 1
    Do you mean jQuery data (http://api.jquery.com/jQuery.data/) or a `data` attribute? Also, what relevance does the code you posted have? – Matt Jun 07 '11 at 07:18

5 Answers5

2

You can i either use RegEx or make use of String Manipulation function like IndexOf...

or

try search() String method.

 var isFound = data.search(new RegExp(/Mydiv/i));

if you are using jQery than here is alredy given answer

jQuery .search() to any string

Find text string using jQuery?

Community
  • 1
  • 1
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • Parsing HTML with regular expressions is fraught with peril. Not recommended. Using `String#indexOf` means any occurrence of "Mydiv" would match, even if not an `id`. – T.J. Crowder Jun 07 '11 at 07:34
1

I assume data is a string containing HTML markup. If so, then:

var tree = $(data);
if (tree.find("#Mydiv")[0]) {
    // An element with the `id` "Mydiv" exists in the tree
}

You don't have to use a variable, you could just do this:

if ($(data).find("#Mydiv")[0]) {
    // An element with the `id` "Mydiv" exists in the tree
}

...but I just assume you're probably going to want to do something else with tree afterward, so we want to avoid parsing it more than once.

Note: If the "Mydiv" element may be at the top level of the HTML in data, you'll want to wrap that structure in a parent element for the above to work (there are other ways, but they're more complicated). So if that may be the case, use $("<div>" + data + "</div>") rather than just $(data). This is because find searches descendant elements, so if the HTML were "hi", it wouldn't find it as it's not a descendant (it's the element itself).

Update: Here's a live example

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • J.Crowder the look not working. In fact, if "Mydiv" id is present in data, the UI behavior change (show/hide other div) – TheBoubou Jun 07 '11 at 07:37
  • @Kris-I: Works for me. Just added a live example. Perhaps if you compare what's different, it'll help you figure it out. – T.J. Crowder Jun 07 '11 at 07:50
0
var result="limitless isa web ...";
if(result.search('limitless')>=0) // or another text Example: web
 {
   alert("ok");
 }
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Limitless isa
  • 3,689
  • 36
  • 28
0

This will do if i understood correctly.

if($("#Mydiv",data).length)
{
    //Mydiv exists
}
Jishnu A P
  • 14,202
  • 8
  • 40
  • 50
0

To the extent of my understanding...

if you are trying to find div with id 'myDiv' in data... you can do this..

function (data) {
        if($("#Mydiv",data)) {
            // this should do
        } 
    })

Be little more clear in explaining what you want....

Milli
  • 1,351
  • 15
  • 19