javascript - How to search JSON in jQuery -
hello have 3 divs contain 1,2,3
<div>1</div> <div>2</div> <div>3</div>
and have json
[{ "factoreid:1": "factoreitems:a, b, c, d, e" }, { "factoreid:2": "factoreitems:g,f" }, { "factoreid:3": "factoreitems:i, k, h" }]
i want when hover on divs values checked.
if div contains 1
show factoreitems of "factoreid:1": "factoreitems:a, b, c, d, e"
, if contains 2
show factoreitems of "factoreid:2": "factoreitems:g,f"
, on ....
the json posted incorrect, fixed "factoreid" : num, "factoreitems": "string"
html
<div>1</div> <div>2</div> <div>3</div>
jquery
//fixed json var factore = [{ "factoreid": 1, "factoreitems": "a, b, c, d, e" }, { "factoreid": 2, "factoreitems": "g,f" }, { "factoreid": 3, "factoreitems": "i, k, h" }] $('div').on('mouseover', function () { $(this).text("factoreid : "+factore[$(this).text() -1].factoreid +"factoreitems"+ factore[$(this).text()-1].factoreitems); });
explanation
factore : array json
$(this).text() -1
returns divs number , -1 since array starts 0
factore[$(this).text() - 1] // if mouseover div text 1 result factore[0] // use .factoreid id value
Comments
Post a Comment