javascript - Recognize in which tag given word is? -
next, have div below tags contains type [info] [/ info]:
<div id="tags"> [info id=“rol”] rol -> greatest project add in same people [/info] [info id=“add”] add -> moment pick ball , shoot in cars [/info] [info id=“tree”] tree -> tree of views in other dimensions [/info] </div>
i created search engine made in jquery, type in text field , click search saves variable 'searchtext', can see below:
$(“#buttongo").click(function() { var searchtext = $(‘#searchfield’).val(); });
after have is:
take value stored in variable searchtext, , text in div
then find text give warning showing in located info tag, eg:
searchtext var = "views in other";
must recognize tag [info id = "tree"] [/ info] because text located within tag .... , return alert should come follows:
(tag: info, id: tree, desc:"tree -> tree of views in other dimensions")
i not know start, can give me hand?
to follow on earlier comment, can create info
object this:
var treeinfo = new object(); treeinfo.id = "tree"; treeinfo.description = "tree -> tree of views in other dimensions"
next, create array hold info objects if desire:
var infoarray = []; infoarray.push(treeinfo);
then when you're checking provided text, use simple loop. eg:
for (var = 0; < infoarray.length; i++) { if (infoarray[i].description.indexof(searchtextvar) != -1){ //found correct text, something. } }
it better wrap for
loop in function , return info object when find match.
you can use path if don't want store info
objects in div.
edit address comment array size: if want use huge block of text can use method below though isn't cleanest/fastest. 1 way of solving problem.
use regex this: \[info id=.+?]
once starting index of searchtextvar
. , then, find closing tag same index \[/info]
. now, substring of searchtextvar
using indices regex matches contain tag.
after that, resulting string should right tag , can parse text , display appropriate.
if you're dealing many tags, might not best idea store them in browser... consider using database or other storage mechanism
Comments
Post a Comment