How to limit Javascript's window.find to a particular DIV?

javascript limit array
limit() javascript
javascript max number
javascript max string value
javascript limit number size
javascript clamp
max of three numbers in javascript
javascript range min, max

Is it possible to use Javascript in Safari/Firefox/Chrome to search a particular div container for a given text string. I know you can use window.find(str) to search the entire page but is it possible to limit the search area to the div only?

Thanks!

Once you look up your div (which you might do via document.getElementById or any of the other DOM functions, various specs here), you can use either textContent or innerText to find the text of that div. Then you can use indexOf to find the string in that.

Alternately, at a lower level, you can use a recursive function to search through all text nodes in the window, which sounds a lot more complicated than it is. Basically, starting from your target div (which is an Element), you can loop through its childNodes and search their nodeValue string (if they're Texts) or recurse into them (if they're Elements).

The trick is that a naive version would fail to find "foo" in this markup:

<p><span>fo</span>o</p>

...since neither of the two Text nodes there has a nodeValue with "foo" in it (one of them has "fo", the other "o").

Why You Should Limit JavaScript, This article was written by Zach Briggs, Senior Developer and JavaScript Practice Lead with UX design and custom software company Table XI  Frontend Development » Why you should limit JavaScript — and how to do it 0 This article was written by Zach Briggs, Senior Developer and JavaScript Practice Lead with UX design and custom software company Table XI

Depending on what you are trying to do, there is an interesting way of doing this that does work (does require some work).

First, searching starts at the location where the user last clicked. So to get to the correct context, you can force a click on the div. This will place the internal pointer at the beginning of the div.

Then, you can use window.find as usual to find the element. It will highlight and move toward the next item found. You could create your own dialog and handle the true or false returned by find, as well as check the position. So for example, you could save the current scroll position, and if the next returned result is outside of the div, you restore the scroll. Also, if it returns false, then you can say there were no results found.

You could also show the default search box. In that case, you would be able to specify the starting position, but not the ending position because you lose control.

Some example code to help you get started. I could also try putting up a jsfiddle if there is enough interest.

Syntax:

window.find(aStringToFind, bCaseSensitive, bBackwards, bWrapAround, bWholeWord, bSearchInFrames, bShowDialog);

For example, to start searching inside of myDiv, try

document.getElementById("myDiv").click(); //Place cursor at the beginning
window.find("t", 0, 0, 0, 0, 0, 0); //Go to the next location, no wrap around

You could set a blur (lose focus) event handler to let you know when you leave the div so you can stop the search.

To save the current scroll position, use document.body.scrollTop. You can then set it back if it trys to jump outside of the div.

Hope this helps! ~techdude

How can I use JavaScript to limit a number between a min/max , like this var number = Math.min(Math.max(parseInt(number), 1), 20);. Live Demo: function limitNumberWithinRange(num, min, max){ const MIN  So how can we prevent JavaScript execution alerts? The best solution is to avoid long-running client-side tasks. Ideally, no event handler should take longer than a few dozen milliseconds.

As per the other answer you won't be able to use the window.find functionality for this. The good news is, you won't have to program this entirely yourself, as there nowadays is a library called rangy which helps a lot with this. So, as the code itself is a bit too much to copy paste into this answer I will just refer to a code example of the rangy library that can be found here. Looking in the code you will find

 searchScopeRange.selectNodeContents(document.body);

which you can replace with

 searchScopeRange.selectNodeContents(document.getElementById("content"));

To search only specifically in the content div.

How to limit a number between a min/max value in JavaScript , Here 2 approaches are discussed with the help of JavaScript. Approach 1: Take the input from the input element and convert it to number using Number() method. Pass a callback function to the function created by the factory function. The callback function will be entered into a queue. The limit function executes the first 10 functions in the queue and then waits until this interval has finished to execute the next 10 functions until the queue is empty. Return the limit function from the factory function.

var elements = [];
$(document).find("*").filter(function () {
  if($(this).text().contains(yourText))
    elements.push($(this));
});
console.log(elements);

I didn't try it, but according the jQuery documentation it should work.

Javascript function limit - JavaScript, Is there a limit to the number of javascript functions which can be included in a javascript source file, e.g. <script type="text/javascript" src = "myscript.js"></script​>  If you wanted to limit the number of characters to 1 you could set a min of 0 and a max of 9. You can also set the stepattribute, which is 1 by default, but would come in use if you wanted the ability to select decimals or other rounded numbers. <input type="number" maxlength="1" max="9" min="1" size="1" />.

Here is how I am doing with jquery:

var result = $('#elementid').text().indexOf('yourtext') > -1

it will return true or false

limit JavaScript and Node.js code examples, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  JavaScript max() Method The max() method returns the number with the highest value. Tip: The min() method returns the number with the lowest value. Browser Support.

JavaScript toFixed() Method, and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML. Javascript function to determine the length of the value of a particular field in a HTML form.

HTML input maxlength Attribute, Javascript - Limit a `.map()` loop to a certain number using `.slice()`- via: https://​stackoverflow.com/questions/42374873/limit-items-in-a-map-loop. limit-map-loop​. jQuery code to limit its size: var elem = $("#myTextfield");if(elem) elem.val(elem.val().substr(0,10)); As an example, you could use the jQuery code above to restrict the user from entering more than 10 characters while he's typing; the following code snippet does exactly this: $(document).ready(function() { var elem = $("#myTextfield"); if (elem) { elem.keydown(function() { if (elem.val().length > 10) elem.val(elem.val().substr(0, 10)); });

Javascript, But if this field is added to edit screen it also can be edited form view of issue and here javascript does not work at all (field could have more than 8 signs and can  The below example shows how to achieve it using JavaScript. Prerequisites. Knowledge of JavaScript. Example with Source Code. We will show here two approaches on how to limit number of checkbox selections using JavaScript. Using Form. The below example shows that maximum of two checkboxes can be selected. The checkboxes are put inside an HTML

Comments
  • I'm actually looking to replicate the browser find functionality (the string would be highlighted and upon clicking search one more time it would move on down to the next match inside of the Div, etc).
  • @VeePee: I'm afraid you'd have to write it (which is possible, just a real pain), even window.find isn't standardized. But again, it's possible, because you can find the text in the Text node, split that node to surround it with a highlighting span, etc., etc.
  • I should add, this does work in firefox/chrome/safari.