1

I have created the funtion below, It search the body for a specific Word, and im able to get the result if it exist or not. But If it exist, how do i then find that specific Object and interract with it.?

(function ($) {

    $.fn.WordBreaker = function (x) {

        return this.each(function () {

            var wrapper = $(this);

            var xx = $.extend({
                words: "",
            }, x || {});

            function initialized() {
                xx.words.forEach(function (x, y) {
                    var lw, rw;
                    lw = x.toString().split(",")[0];
                    rw = x.toString().split(",")[1];

                    if ($("body:contains('" + lw + "')").length > 0) {
                       alert("I found an object that contains: " + lw + " , but how do i tager that object?")
                    }

                }, xx.words)
            }
            initialized();
        });
    }

}(jQuery));

var items = [
["THISISALONGTEXTTHATIWANTTOBREAK", "THIS-IS-A-LONG-TEXT-THAT-I-WANT-TO-BREAK"]
];
$('.col-md-5').WordBreaker({ words: items })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
            <div class="col-md-5" style="background: #00ffff">
                <h1>THISISALONGTEXTTHATIWANTTOBREAK</h1>
            </div>
        </div>
  • What could that specific object be? Div/Span/Link/Paragraph/...? If you know that it is only in a div, you could search all divs: $(div).each(function() { /* search content of div here */ }); – Seb Dec 06 '16 at 14:23
  • It could be anything. any kind of object that can contain text basic. but i would like it to be as dynamic as posible –  Dec 06 '16 at 14:24
  • So it could be the alt-text of an image? Or even in a comment? You could try to use a regexp to get the tag bevor the text. (But it would not help if this is a
    .
    – Seb Dec 06 '16 at 14:27

1 Answers1

0

For this answer, I've taken the code from Using jQuery is there a way to find the farthest (deepest, or most nested) child element? and put it in here. The original answer is from @Methos.

(function ($) {

    $.fn.WordBreaker = function (x) {

        return this.each(function () {

            var wrapper = $(this);

            var xx = $.extend({
                words: "",
            }, x || {});

            function initialized() {
                xx.words.forEach(function (x, y) {
                    var lw, rw;
                    lw = x.toString().split(",")[0];
                    rw = x.toString().split(",")[1];
                    var all_matched_elements = $(":contains('" + lw + "')");
                    var all_parent_elements = $(all_matched_elements).parents();
                    var all_deepest_matches = $(all_matched_elements).not(all_parent_elements);

                    console.log(all_deepest_matches); // this is an object containing the deepest objects that match the search string

                }, xx.words)
            }
            initialized();
        });
    }

}(jQuery));

var items = [
["THISISALONGTEXTTHATIWANTTOBREAK", "THIS-IS-A-LONG-TEXT-THAT-I-WANT-TO-BREAK"]
];
$('.col-md-5').WordBreaker({ words: items })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
            <div class="col-md-5" style="background: #00ffff">
                <h1>THISISALONGTEXTTHATIWANTTOBREAK</h1>
            </div>
        </div>
Community
  • 1
  • 1
Chris Lear
  • 6,592
  • 1
  • 18
  • 26