Post Reply 
 
Thread Rating:
  • 2 Votes - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Style by element class, content by element id
20th December, 13:55
Post: #1
Style by element class, content by element id
Hey, I just wanted to post the method I used to set default styles for a group of tips using their class, but set content individually using their ids.

I had a look around the forums for a better method but couldn't find exactly what I was looking for. If someone knows a better way, please advise.

I was basically not happy with having to repeat the settings for style and behavior for each element to also set their content. I wanted just one declaration for that stuff so I could change it easily and leave the content as separate.

In the doc head, I use an associative array containing element IDs and their content, then a for each loop selecting elements by class (.qtip) and applying generic styles and the specific content where the id matches:

JS Code
var contents = new Array();
  contents['tip-first'] = { url: 'link1.html' };
  contents['tip-second'] = {url: 'link2.html'};
  contents['tip-other'] = '<p>Some HTML text content</p>';
 
$(".qtip").each( function(i) {
   $(this).qtip({
   position: { corner: { target: 'rightMiddle', tooltip: 'leftMiddle' } },
   style: { name: 'cream', tip: true, width: { max: 480 } },
   hide: { when: 'mouseout', fixed: true },
   content: contents[$(this).attr('id')] // Use the current element id value to grab the content for this particular tooltip 
   });
});


My tooltip elements look like this:

JS Code
<a class="qtip" id="tip-first" href="#" o<strong></strong>nclick="return(false)">The first one</a>
<a class="qtip" id="tip-second" href="#" o<strong></strong>nclick="return(false)">The second one</a>
<a class="qtip" id="tip-other" href="#" o<strong></strong>nclick="return(false)">Another one</a>


Hope it helps.
Visit this user's website Find all posts by this user
Quote this message in a reply
19th February, 01:49
Post: #2
RE: Style by element class, content by element id
Just noticed an error - don't know why it put strong tags after the o in onclick? I can't get it to show the code without them though.

(20th December 13:55)teeks Wrote:  
JS Code
<a class="qtip" id="tip-first" href="#" o<strong></strong>nclick="return(false)">The first one</a>
<a class="qtip" id="tip-second" href="#" o<strong></strong>nclick="return(false)">The second one</a>
<a class="qtip" id="tip-other" href="#" o<strong></strong>nclick="return(false)">Another one</a>

Each link should be
JS Code
<a class="qtip" id="tip-id" href="#">Tip Link</a>

The onclick was just to stop it from appending the # to the URL for testing with internal links.
Visit this user's website Find all posts by this user
Quote this message in a reply
24th February, 22:04 (This post was last modified: 24th February 22:05 by speshulk926.)
Post: #3
RE: Style by element class, content by element id
You are THE MAN! Your example was *exactly* what I was wanting to do. I really appreciate it, because I was getting frustrated on how to achieve this!! YAY! Smile
Find all posts by this user
Quote this message in a reply
26th February, 01:18
Post: #4
RE: Style by element class, content by element id
(24th February 22:04)speshulk926 Wrote:  You are THE MAN! Your example was *exactly* what I was wanting to do. I really appreciate it, because I was getting frustrated on how to achieve this!! YAY! Smile
Cheers. I feel warm and fuzzy now. This is the first time I've contributed back to anything, usually I'm under too much pressure to move on to the next job. I'll try and continue this way though, its more rewarding. Smile
Visit this user's website Find all posts by this user
Quote this message in a reply
26th February, 06:39
Post: #5
RE: Style by element class, content by element id
The class attribute, on the other hand, assigns one or more class names to an element; the element may be said to belong to these classes. A class name may be shared by several element instances. The class attribute has several roles in HTML:
As a style sheet selector (when an author wishes to assign style information to a set of elements).
For general purpose processing by user agents.
Find all posts by this user
Quote this message in a reply
27th February, 03:44
Post: #6
RE: Style by element class, content by element id
(26th February 06:39)james121 Wrote:  The class attribute, on the other hand, assigns one or more class names to an element; the element may be said to belong to these classes. A class name may be shared by several element instances. The class attribute has several roles in HTML:
As a style sheet selector (when an author wishes to assign style information to a set of elements).
For general purpose processing by user agents.

I thought this post was weird, it has nothing to do with the thread and smells like something a bot would say, so I googled it and its copied exactly from http://www.w3.org documentation. Report spam?
Visit this user's website Find all posts by this user
Quote this message in a reply
27th February, 22:07
Post: #7
RE: Style by element class, content by element id
(27th February 03:44)teeks Wrote:  I thought this post was weird, it has nothing to do with the thread and smells like something a bot would say, so I googled it and its copied exactly from http://www.w3.org documentation. Report spam?

Thanks teeks for the heads up, does seem very bot-like. I've warned him and sent a PM.

Craig Thompson
Web Developer / Designer
Craigsworks
http://www.craigsworks.com
Visit this user's website Find all posts by this user
Quote this message in a reply
15th March, 22:49
Post: #8
RE: Style by element class, content by element id
It did n,t work for me:
here is what I did:

JS Code
<head>
<script type="text/javascript" src="js/qtip/jquery-1.3.2.min.js"></script>
 <script type="text/javascript" src="js/qtip/jquery.qtip-1.0.0-rc3.min.js"></script>
<script type="text/javascript">
$(document).ready(function() 
var contents = new Array();
  contents['tip-first'] = { url: 'link1.html' };
  contents['tip-second'] = {url: 'link2.html'};
  contents['tip-other'] = '<p>Some HTML text content</p>';
 
$(".qtip").each( function(i) {
   $(this).qtip({
   position: { corner: { target: 'rightMiddle', tooltip: 'leftMiddle' } },
   style: { name: 'cream', tip: true, width: { max: 480 } },
   hide: { when: 'mouseout', fixed: true },
   content: contents[$(this).attr('id')] // Use the current element id value to grab the content for this particular tooltip 
   });
});
</script>
</head>


the tool tips :
JS Code
<a class="qtip" id="tip-first" href="#">The first one</a>
<a class="qtip" id="tip-second" href="#">The second one</a>
<a class="qtip" id="tip-other" href="#">Another one</a>



thanks for your help[/quote][/php]
Find all posts by this user
Quote this message in a reply
17th March, 00:23
Post: #9
RE: Style by element class, content by element id
(15th March 22:49)insideout Wrote:  It did n,t work for me...

Your code looks right. Its hard to tell with just a snippet though. Can you post a URL where you are getting errors and describe what's going wrong in more detail?
Visit this user's website Find all posts by this user
Quote this message in a reply
24th March, 09:06
Post: #10
RE: Style by element class, content by element id
The below code does not work at all... can someone tell me how to get it working?


(15th March 22:49)insideout Wrote:  It did n,t work for me:
here is what I did:

JS Code
<head>
<script type="text/javascript" src="js/qtip/jquery-1.3.2.min.js"></script>
 <script type="text/javascript" src="js/qtip/jquery.qtip-1.0.0-rc3.min.js"></script>
<script type="text/javascript">
$(document).ready(function() 
var contents = new Array();
  contents['tip-first'] = { url: 'link1.html' };
  contents['tip-second'] = {url: 'link2.html'};
  contents['tip-other'] = '<p>Some HTML text content</p>';
 
$(".qtip").each( function(i) {
   $(this).qtip({
   position: { corner: { target: 'rightMiddle', tooltip: 'leftMiddle' } },
   style: { name: 'cream', tip: true, width: { max: 480 } },
   hide: { when: 'mouseout', fixed: true },
   content: contents[$(this).attr('id')] // Use the current element id value to grab the content for this particular tooltip 
   });
});
</script>
</head>


the tool tips :
JS Code
<a class="qtip" id="tip-first" href="#">The first one</a>
<a class="qtip" id="tip-second" href="#">The second one</a>
<a class="qtip" id="tip-other" href="#">Another one</a>



thanks for your help
[/php]
[/quote]
Find all posts by this user
Quote this message in a reply
24th March, 12:46
Post: #11
RE: Style by element class, content by element id
(24th March 09:06)junkiexl09 Wrote:  The below code does not work at all... can someone tell me how to get it working?


(15th March 22:49)insideout Wrote:  It did n,t work for me:
here is what I did:

JS Code
<head>
<script type="text/javascript" src="js/qtip/jquery-1.3.2.min.js"></script>
 <script type="text/javascript" src="js/qtip/jquery.qtip-1.0.0-rc3.min.js"></script>
<script type="text/javascript">
$(document).ready(function() 
var contents = new Array();
  contents['tip-first'] = { url: 'link1.html' };
  contents['tip-second'] = {url: 'link2.html'};
  contents['tip-other'] = '<p>Some HTML text content</p>';
 
$(".qtip").each( function(i) {
   $(this).qtip({
   position: { corner: { target: 'rightMiddle', tooltip: 'leftMiddle' } },
   style: { name: 'cream', tip: true, width: { max: 480 } },
   hide: { when: 'mouseout', fixed: true },
   content: contents[$(this).attr('id')] // Use the current element id value to grab the content for this particular tooltip 
   });
});
</script>
</head>


the tool tips :
JS Code
<a class="qtip" id="tip-first" href="#">The first one</a>
<a class="qtip" id="tip-second" href="#">The second one</a>
<a class="qtip" id="tip-other" href="#">Another one</a>



thanks for your help
[/php]
[/quote]

Well seeing as you quoted the code that insideout already said didn't work, that's not surprising.
You need to post what kind of error you're having or your exact code or a URL where people can see what's happening otherwise its a little hard to debug based on: it doesn't work.
Visit this user's website Find all posts by this user
Quote this message in a reply
Yesterday, 05:30
Post: #12
RE: Style by element class, content by element id
Hello this method works for me perfectly with:

JS Code
contents['selector'] = { url: 'link.php?id=' + id };

My issue now is that the qtip opens with the correct data but like a flying box randomly from any position in direction to the supposed postion. If I just replace the dynamic data for static string this doen't happens. Any suggestion or issue for position / dynamic data using this type of method? Huh
Find all posts by this user
Quote this message in a reply
Yesterday, 07:03
Post: #13
RE: Style by element class, content by element id
(Yesterday 05:30)alfonsomr Wrote:  Hello this method works for me perfectly with:

JS Code
contents['selector'] = { url: 'link.php?id=' + id };

My issue now is that the qtip opens with the correct data but like a flying box randomly from any position in direction to the supposed postion. If I just replace the dynamic data for static string this doen't happens. Any suggestion or issue for position / dynamic data using this type of method? Huh

I had the same problem using google maps in a tip, probably just because it has to figure out redering position and size on the fly. Maybe having default static content, then replacing it dynamically as required, would at least give it something to render?? Static content could be "loading..." or something, because people won't see it anyway right?
Visit this user's website Find all posts by this user
Quote this message in a reply
Yesterday, 13:36
Post: #14
RE: Style by element class, content by element id
Thank you Teeks for the suggestion. I did that, the static text ("Loading...") appear correctly but when it is replaced by the dynamic data, the position of the tip is affected because the dynamic text is longer... the height of the box increased.
Find all posts by this user
Quote this message in a reply
15th November, 21:35 (This post was last modified: 15th November 21:36 by garbledygook.)
Post: #15
RE: Style by element class, content by element id
(26th February 06:39)james121 Wrote:  The class attribute, on the other hand, assigns one or more class names to an element; the element may be said to belong to these classes. A class name may be shared by several element instances. The class attribute has several roles in HTML:
As a style sheet selector (when an author wishes to assign style information to a set of elements).
For general purpose processing by user agents.

I've also used solutions that simply use the each() function, and then just do a switch on the attr('id') for the current element matching my each loop. This comes in handy if you are using a lot of variable text qualifiers on your elements for descriptive purposes. Then based on that you can change default values for variables, then simply call a function like AddQtip passing in the appropriate changes and be done with it. If you wanted, I'm guessing you could make some kind of struct to use, etc.
Find all posts by this user
Quote this message in a reply
Post Reply 


Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  [Solved] how to set an ID for the qtip generated element buntu 8 3,231 16th February 09:53
Last Post: johannapelkonen
  How to global a style for each qtip method? kmvan 1 989 25th September 13:28
Last Post: Craig
  [Solved] Getting tooltip to appear on different element with the same class name andrewhoule 5 1,450 25th August 18:17
Last Post: Craig
  [Solved] Style Update on validation ni8hood 1 985 25th July 11:45
Last Post: Craig
  [Solved] Take tooltip content from the element attributes AndyG 17 16,083 31st January 07:14
Last Post: eggpoker
  qTip Position and Style Adjustments cohlerm 1 1,261 25th August 17:09
Last Post: Craig
  Apply new class to tooltip when position has been adjusted beforeseven 4 2,032 9th June 16:43
Last Post: beforeseven
  something like style { tip: "auto" } nemoluca 3 1,151 15th April 16:27
Last Post: Craig
  programatically show tip in different element jorfermo 8 2,432 31st March 12:20
Last Post: jorfermo
  Use hovered element's own HTML for qtip content discern 2 3,721 10th December 13:52
Last Post: discern



User(s) browsing this thread: 1 Guest(s)