Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to insert text into an array based upon keywords with JQuery
    primarykey
    data
    text
    <p>Within a web project I am using jQuery and javascript to build a query string for a user to use within a search tool. Since this is built on the fly by the user I take the fields and the values from the form and then add this to an array which is then pushed over to the search term in the ui. </p> <p>Prior to passing this string to the UI I would like to format the string based upon keywords. (Example if the keyword BETWEEN all caps) is used in the query wrap the input fields following this keyword (there will be two) in parentheses. If the term CONTAINS is used wrap the succeeding field with asterisk and so on. Within JavaScript there is function for slice which allows me to insert text at an index within an array. Since I am looking for keywords within the array and not index values is there another function or approach that would support this.</p> <p>below is the code for the creation/parsing of the input fields into the search term </p> <pre><code>//insert field after between option or remove it if it exists function insertFromField(param1, param2, param3) { if (param1 == "BETWEEN") { $("&lt;input class='queryterm' type='text'name='fieldValue1' id='fieldvalue1' value='' size='15' /&gt;&lt;span&gt;&amp;nbsp;TO&amp;nbsp;&lt;/span&gt;").insertAfter(param2); } if (param3 &lt;= 2) { // do nothing } else { $(param2).next().remove(); $(param2).next().remove(); } } //build query string from Query Builder //TODO formatting on string return function setQueryString() { checkField(); var txt = $("input[name='tbsearchterm']"); var fields = $("#queryFields :input").serializeArray(); jQuery.each(fields, function (i, field) { if (txt) { txt.val(txt.val() + field.value + " "); } }); } //the UI contains an add button so the user can build their query at runtime for that i have the following: $('#add').click(function () { $("&lt;div class='additionalrow'&gt;&lt;select class='queryterm' name='condition'&gt;&lt;option value='AND'&gt;AND&lt;/option&gt;&lt;option value='OR'&gt;OR&lt;/option&gt;&lt;/select&gt;&amp;nbsp;&amp;nbsp;"+ "&lt;select class='queryterm' name='condition'&gt;" + "&lt;option value='address:'&gt;Address&lt;/option&gt;" + "&lt;option value='age:'&gt;Age&lt;/option&gt;" + ... edited for Brevity "&lt;/select&gt;&amp;nbsp;&amp;nbsp;&lt;select name='operator'class='operClass'&gt;&lt;option id='opt1'&gt;Equals&lt;/option&gt;&lt;option id='opt1' value='CONTAINS'&gt;Contains&lt;/option&gt;&lt;option id='opt2' value='DOES NOT CONTAIN'&gt;Does Not Contain&lt;/option&gt;&lt;option id='opt3' value='LIKE'&gt;Like&lt;/option&gt;&lt;option id='opt4' value='BETWEEN'&gt;Between&lt;/option&gt;&lt;/select&gt;&amp;nbsp;&amp;nbsp;&lt;input type='text' class='queryterm' name='fieldValue1' id='fieldvalue1' value='' size='25' /&gt;&amp;nbsp;&amp;nbsp;&lt;a href='#' id='btnRemove'&gt;&lt;span class='advButtons'&gt;Remove&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;").appendTo('#queryFields'); }); </code></pre> <p>Thanks in advance</p> <p><strong>Update to comments</strong> Thanks for the reply for more clarification: 1. I am hoping to accomplish this preferably using jQuery but I am not opposed to using javascript if jQuery does not contain functions that support this in.</p> <p>2 &amp; 3. Contains will always have one value . Where contains will always have two. The function insertFromField is called when the selection is changed on the selector drop down menu</p> <p>A full working sample can be viewed here <a href="http://jsfiddle.net/rlcrews/Q4cV8/2/" rel="nofollow">jsFiddle</a></p> <p>When you change the second selector you will see the additional input field added. This is triggered by the insertFromField function. This basically evaluates the entire div element and counts the number of input fields and adds or removes one accordingly. </p> <p>I have also added some code to allow you to view the result in the UI.</p> <p>The intended output would be when the word BETWEEN is used place parentheses around the values of the last two input field so it would look something like this: AND Address 123 main street BETWEEN (17 18) </p> <p>Sorry for not thinking of Fiddle earlier</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload