blacknight Posted September 24, 2012 Share Posted September 24, 2012 ok im working on making a tooltip script using jquery... my tooltips are stored un this type of fashion.. <script type="text/javascript"> <!--//--><![CDATA var tiplib_1 = "Search items and recipes in the database"; var tiplib_2 = "Roster Configuration Panel"; var tiplib_3 = "Add a new guild and synchronize<br \/>the memberlist with Blizzard\'s Armory"; var tiplib_4 = "Image screenshot database"; //--><!]]> </script> this is located at the bottom of my page .. my question is how would i call this info so i can place it in a div container <div data-tip="tiplib_1" >This is link #4</div> $(document).ready(function(){ simple_tooltip("[data-tip]","tooltip"); }); starts mt script function simple_tooltip(target_items, style){ $(target_items).each(function(i){ var Tip_name = $(this).attr("data-tip"); //$("body").append("<div id='"+$(this).attr("data-tip")+"'>"+$($(this).attr("data-tip"))+"</div>"); var myar = Tip_name.split('_'); alert("id "+i+"- "+$(this).attr("data-tip")+"-"+myar[1]+""); $("<div id='"+Tip_name+"'>"+$(this).attr("data-tip")+"</div>").appendTo("body") $("#"+Tip_name).html(Tip_name); var my_tooltip = $("#"+Tip_name); Tip_name has the name of the var i wana call but for the life of me i cannoe make use get the value of the var and put it in the div any help? Quote Link to comment https://forums.phpfreaks.com/topic/268757-variables-in-jquery/ Share on other sites More sharing options...
kicken Posted September 24, 2012 Share Posted September 24, 2012 If you're going to store the tips separately like that, then use an array, not just sequentially numbered variables. However, why don't you just store the tooltip text directly in the data attribute (or the title attribute) and not even bother with the array: //<div data-tip="Search items and recipes in the database">blah blah</div> function simple_tooltip(target_items, style){ $(target_items).each(function(i){ var tipText = $(this).attr('data-tip'); var tipDiv = $("<div>"); tipDiv.appendTo(document.body).html(tipText); } } Completely untested, but something like that. Quote Link to comment https://forums.phpfreaks.com/topic/268757-variables-in-jquery/#findComment-1380690 Share on other sites More sharing options...
blacknight Posted September 24, 2012 Author Share Posted September 24, 2012 Thanks for the reply, the reason they are stored as such is because they contain html tho the code had the same result as befor iy just put tiplib_1 in the div not the value of it.. Quote Link to comment https://forums.phpfreaks.com/topic/268757-variables-in-jquery/#findComment-1380704 Share on other sites More sharing options...
blacknight Posted September 24, 2012 Author Share Posted September 24, 2012 seems i have solved the issue some what using eval( $(this).attr('data-tip')); it uses the data it needs too Quote Link to comment https://forums.phpfreaks.com/topic/268757-variables-in-jquery/#findComment-1380707 Share on other sites More sharing options...
kicken Posted September 25, 2012 Share Posted September 25, 2012 (edited) the reason they are stored as such is because they contain html That's not an issue. You just have to escape the HTML properly. Eg: <div data-tip="<h1>Some title</h1><p>This is the tooltip "content"</p>">Something</div> Like I said though, if you want to keep them separate, use an array: <script type="text/javascript"> var tooltipStrings = [ "Search items and recipes in the database", "Roster Configuration Panel", "Add a new guild and synchronize<br \/>the memberlist with Blizzard\'s Armory", "Image screenshot database" ]; </script> Then set the index # as the data attribute, eg <div data-tip="2">blah</div> And finally to get the string you'd do: var tipIndex=$(this).attr('data-tip'); var tipText = tooltipStrings[tipIndex]; Using eval is about the worst possible thing you could do. Edited September 25, 2012 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/268757-variables-in-jquery/#findComment-1380726 Share on other sites More sharing options...
kicken Posted September 25, 2012 Share Posted September 25, 2012 Working (in chrome at least) example: http://jsfiddle.net/YNHXM/ Quote Link to comment https://forums.phpfreaks.com/topic/268757-variables-in-jquery/#findComment-1380727 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.