Question: I've been trying to call a user defined JavaScript function on a jQuery element, and though I've seen several topics on here about it, I'm still having a bit of trouble applying their logics to my situation.
Here's what i've tried so far:

**Javscript & jQuery:**

```
    $(document).ready(function() {

// JavaScript function     
     nl2br = function (str, is_xhtml) {
            var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br ' + '/>' : '<br>';
            return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');
        }

    // jQuery Call
        if ( ! $('.mediaDesc:contains("p")').length || ! $('.mediaDesc:contains("div")').length || ! $('.mediaDesc:contains("span")').length ) {
                nl2br(this);
            }

    }); //end document ready

```

**HTML:**

```
<div class="mediaDesc">
Hello World!
I am text on a new line soon to have a br tag before me!    
</div>

```

My goal is to dynamically find div's with a class of **mediaDesc** to see if their inner content contains html tags such as p,span, or div. If not, then apply the nl2br JavaScript function to it.
Any help would be appreciated.
Thanks in advance.

Answer: 
Based on the current available info, I believe you want something like this:

```
$(".mediaDesc").each(function () {
    if ($(this).find("p").length == 0 || $(this).find("div").length == 0 || $(this).find("span").length == 0)
    {
        $(this).html(nl2br($(this).html()));
    }
});

```

edit: fixed mis-use of `.contains`

<http://jsfiddle.net/s8eNh/>
`.contains` is for searching text within the node. What you want is:

```
$('.mediaDesc').not(':has(p, div, span)').each(nl2br)

```

if you need to process the node and only call nl2br with `text` or `html` then do:

```
$('.mediaDesc').not(':has(p, div, span)').each(function(){
    nl2br( $(this).html() )
})

```