Jump to content

Variables In Jquery....


blacknight

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/268757-variables-in-jquery/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/268757-variables-in-jquery/#findComment-1380690
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/268757-variables-in-jquery/#findComment-1380726
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.