I am using Bootstrap v3.3.5 and JQuery v1.11.3 .
I have a select list that when a value is selected, I would like the placement and css class of a tooltip to change.
I have searched Google & SO and read many SO posts, but the many attempts I have made fail. For example, this did not work for me.
Here is my html code:
<select id="id_cover_letter_type" name="cover_letter_type">
<option value="0" selected="selected">I will write my own data</option>
<option value="1">Standard Cover Letter</option>
....
</select>
<i id="id_icon_id_cover_letter_details_second_paragraph" class="fa fa-lightbulb-o blue_color icon_size26 verticalAlignTop" data-original-title="" title="" aria-describedby="tooltip229278"></i>
Here is my jquery code:
I have included the commented out attempts that I have made to change the data-placement of the tooltip (taken from other posts) that did not work for me.
$("#id_icon_id_cover_letter_details_second_paragraph").tooltip({'html': true, 'placement': 'bottom', 'title': '{% trans "Paragraph 2" %} <br /><br /> {% trans "This should detail why you want the job." %} <br /><br /> {% trans "Explain how your qualifications and career plan match the advertised position." %} <br /><br /> {% trans "The details you provide here should display that you have researched the employer, what the employer and industry are seeking and that you understand what the job requires." %}'});
if ( $('#id_cover_letter_type').val() == '0' ) {
// the following code did not work.
//$('#id_icon_id_cover_letter_details_second_paragraph').data('bs.tooltip').options.placement = 'left';
// the following code did not work.
//$('#id_icon_id_cover_letter_details_second_paragraph').tooltip({ placement: 'left' });
// the following, destroying, appling the placement and then showing the tooltip did not work.
//$('#id_icon_id_cover_letter_details_second_paragraph').tooltip('destroy');
//$("#id_icon_id_cover_letter_details_second_paragraph").tooltip({'placement':'left'});
//$("#id_icon_id_cover_letter_details_second_paragraph").tooltip('show');
// applying the placement and the showing the tooltip did not work.
//$("#id_icon_id_cover_letter_details_second_paragraph").tooltip({placement : 'left'}).tooltip('show');
} else {
// the following code did not work.
//$('#id_icon_id_cover_letter_details_second_paragraph').data('bs.tooltip').options.placement = 'bottom';
// the following code did not work.
//$('#id_icon_id_cover_letter_details_second_paragraph').tooltip({ placement: 'bottom' });
// the following, destroying, appling the placement and then showing the tooltip did not work.
//$('#id_icon_id_cover_letter_details_second_paragraph').tooltip('destroy');
//$("#id_icon_id_cover_letter_details_second_paragraph").tooltip({'placement':'bottom'});
//$("#id_icon_id_cover_letter_details_second_paragraph").tooltip('show');
// applying the placement and the showing the tooltip did not work.
//$("#id_icon_id_cover_letter_details_second_paragraph").tooltip({placement : 'bottom'}).tooltip('show');
}
Also, if someone can tell me the answer to change the css of the tooltip to class name custom-tooltip-styling123, that would be fantastic.
EDIT
Here is a jsfiddle. This is only my 2nd fiddle, and I don't know how to add in the bootstrap to make the tooltip display.
The main problem
You forgot to add id="id_icon_id_cover_letter_details_second_paragraph" to your img element.
Also you need to wrap your js code by the following one:
$(function () { // On document ready wrapper
// your code ...
});
And the last point is to add tooltip initialization:
$('#id_icon_id_cover_letter_details_second_paragraph').tooltip();
For full code just check this jsfiddle
How to change css of tooltip
There are several solutions you can find on stackoverflow, like this for example.
tooltip has option template which default value is:
'<div class="tooltip" role="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>'
You can add CUSTOM_CSS and pass updated version of this option, for example:
'<div class="tooltip CUSTOM_CSS" role="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>'
Or you can define your own css rules for .tooltip class.
Actually there are more solutions. It's your choice, choose one of them. You know html structure and classes of tooltip element, so manipulate it as you wish.
For more info about template option requirements check the official documentation here
Related
I have been using Cluetip for 8 years. Was asked to spiff up the tips and realized it was abandonded 5 years ago. Tooltipster looks like its actively maintained and feature complete.
I have included the css and js files, no error on loading.
My html is generated by an xslt process but the html looks OK
<span class="handShift" data-tooltip-content="#ad1e88"> ⇉ </span>
<div class="tooltip_templates" style="display:none"><span class="tip" id="ad1e88"><span class="clueTitle">Handshift:</span><span class="clueNote">unknown</span></span></div>
<span class="handShift" data-tooltip-content="#ad1e92"> ⇉ </span>
<div class="tooltip_templates" style="display:none"><span class="tip" id="ad1e92"><span class="clueTitle">Handshift:</span><span class="clueNote">Mary Baker Eddy</span></span></div>
my js init call is
$('span.handShift').tooltipster({
animation: 'fade',
delay: 200,
trigger: 'click',
debug:true
});
Once the page loads the two span.handShift elements have "tooltipstered" added to the class attribute.
I'm using the data-tooltip-content to specify what html is in the tip but I tried using the title attribute with the same results.
When I click on the text in the span.handShift the element identified with the data-tooltip-content attribute disapears from the DOM, no tip is displayed, no error in the console. The element never returns,that I can see. Obviously something is going on as the span is removed from the DOM, but its not what is expected?
I'm using jquery 2.1.4
I've tested, your code works.
cf my jsfiddle http://jsfiddle.net/xpvt214o/405260/
Don't forget to include the main JS :
<script type="text/javascript"
src="http://iamceege.github.io/tooltipster/dist/js/tooltipster.bundle.js"></script>
In my app.js file I have classes that are data driven, such as text and picture classes. I have a hyperlink class for which I used Href that looks like this:
div class = "links" ng-if="field.fieldLink">
<a ng-if="content.LinkField.fieldLinkNewTab !== false" target="_blank"
ng-href="{{ content.fieldLink.fieldLinkHref }}">{{
content.fieldLink.fieldLinkText }}</a>
<a ng-if="content.fieldLink.fieldLinkNewTab === false" ng-href="{{
content.fieldLink.fieldLinkHref }}">{{
content.fieldLink.fieldLinkText }} </a>
</div>
So this way I can easily use it like this in my .JS file:
fieldLink
{
fieldLinkHref: "www.etc.com",
fieldLinkText: "click here for random website",
The problem that I am having is making a field for an image:
fieldLinkImage: "documents/pictures/etc.jpg"
fieldLinkHref: "www.etc.com",
Clicking the picture should redirect me to the url.
I can do this in my .html file just fine, by simply wrapping the image in the class, but I want to select the image in my .JS file.
How do I make this happen without hard coding the links and images in the .html file ?
Thank you!
Sorry about this but I will criticize your code a bit.
First to point out that you have a typo, LinkField should probably be fieldLink in your first <a> element.
Next, why are you comparing !== false when you can just check if var is true or truthy (just put variable in condition - no need to compare with anything - if it's there or is true it will be truthy).
Also to create a new element just because you need to have different attribute is bad, you will get tons of code and get lost at some point. Instead use ng-attr-target which will give you same thing in one line - puts target (or any other) attribute based on condition.
But all of that can be fixed of course, I will take a wild guesses on your data structure during this since you haven't provided jsfiddle or similar. I guess that you have a list of objects that hold images or links and you want to put them together in some ng-repeat based on the type either show link or image.
So this would be your data object:
let contentObj = [{
fieldLink: {
fieldLinkNewTab: false,
fieldLinkHref: "www.etc.com",
fieldLinkText: "click here for random website",
}
}, {
fieldLink: {
fieldLinkNewTab: true,
fieldLinkHref: "www.etc.com",
fieldLinkImage: "https://images.pexels.com/photos/590490/pexels-photo-590490.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb",
}
}];
and this would be your html, if fieldLinkText is there it will show text, if fieldLinkImage is there it will show image, keep in mind if you have both it will show both, also by utilizing power of ng-attr-target you show one element in html and not two with ng-if:
<div class="links" ng-repeat="content in contentObj" ng-if="content.fieldLink">
<a ng-attr-target="{{(content.fieldLink.fieldLinkNewTab) ? '_blank' : undefined}}" ng-href="{{ content.fieldLink.fieldLinkHref }}">
<span ng-if="content.fieldLink.fieldLinkText">{{ content.fieldLink.fieldLinkText }}</span>
<img class="image-class" ng-if="content.fieldLink.fieldLinkImage" src="{{content.fieldLink.fieldLinkImage}}" alt="" />
</a>
</div>
I hope this is helpful, I didn't mean to be too critic, if I was sorry about that, but these things will help you in future. And here's the fiddle that you can play on change your data tweak it up a bit: https://jsfiddle.net/pegla/j392Lvdp/3/
I've been trying jQuery Tooltip from Bootstrap, which requires quite a few extra attributes:
<a rel="tooltip" data-placement="right" data-original-title="Hello" href="#">Tooltip Example</a>
There seems to be a bit of a problem with this though, because I use rel="nofollow" on quite a few URLs I want to use tooltip on, and it doesn't let me use both at once.
Even more important, I want an elegant fallback for users that don't have JavaScript enabled browsers.
I want to know if there's a way I can have jQuery Tooltip treat title="" attribute as data-original-title, and have a default preset for data-placement.
Fiddle: http://jsfiddle.net/FhGT3/
of course there is. you just add title="" and it will pick the text as tooltip.
<a rel="tooltip" data-placement="right" title="my title"
data-original-title="Hello" href="#">Tooltip Example</a>
Here's a sample:
http://jsfiddle.net/sunnykumar08/FhGT3/4/
Also for your second question: Yes, you can set a default preset for data-placement. You just need to specify "data-placement:'
Thanks!
You can call tool tips directly. Lets say you give all the <a> tags you want to have tool tips a class runtooltip
Something
just before the </body> you have
<script>
$('.runtooltip').each(function () {
var $this = $(this);
var options = {
placement: 'top'
}
$this.tooltip();
});
</script>
you can see the jsfiddle here http://jsfiddle.net/8gQ7L/
im not much of a coder. just sort of self taught through wordpress. i have a site I've hacked together from a blank theme and I've taken it pretty far.
the last step i want to do is have my #brick open the permalink page with either rel, or class of fancybox.
using WP fancybox plugin.
here is what I'm doing to make the div clickable in the first place
<div id="brick" onclick="location.href='<?php the_permalink(); ?>';" style="cursor:pointer;">
where in this line would i add rel="fancybox" or class="fancybox"?
thank you in advance.
Since you already have the href set in your <a> tag, like this part of your code.
<h2 class="entry-title"><a rel="bookmark" title="Permalink to Art and commerce in the digital age" href="http://throughthelattice.com/art-and-commerce-in-the-digital-age/">Art and commerce in the digital age</a></h2>
... then, I would do :
Remove this line
<script src="//throughthelattice.com/wp-content/themes/lh_wordpress_blank_theme/js/jquery-1.7.1.min.js"></script>
... you already loaded jQuery v1.8.3 in your <head> section
Replace all id="brick" by class="brick"
Replace this script
<script>
$('#brick').fancybox();
</script>
... by this :
<script>
jQuery(document).ready(function () {
jQuery("a[rel='bookmark']").fancybox({
"type": "iframe"
});
}); // ready
</script>
See JSFIDDLE
Rather straight forward to add rel or class properties to an element:
<div id="brick" onclick="location.href='<?php the_permalink(); ?>';" style="cursor:pointer;" rel="fancybox" class="fancybox">
And the JavaScript could be:
$("div.fancybox").fancybox();
or whatever selector you'd like to use. Since you can select with jQuery using a combination of class, id or tag name.
If I check official documentation, I can see a property called HTML:
Name | Type | default | Description
----------------------------------------------------------------------------
html | boolean | false | Insert html into the tooltip.
If false, jquery's text method
will be used to insert content
into the dom. Use text if you're
worried about XSS attacks.
It says, "insert html into the tooltip", but the type is boolean. How can I use complex html inside a Tooltip?
This parameter is just about whether you are going to use complex html into the tooltip. Set it to true and then hit the html into the title attribute of the tag.
See this fiddle here - I've set the html attribute to true through the data-html="true" in the <a> tag and then just added in the html ad hoc as an example.
Another solution to avoid inserting html into data-title is to create independant div with tooltip html content, and refer to this div when creating your tooltip :
<!-- Tooltip link -->
<p><span class="tip" data-tip="my-tip">Hello world</span></p>
<!-- Tooltip content -->
<div id="my-tip" class="tip-content hidden">
<h2>Tip title</h2>
<p>This is my tip content</p>
</div>
<script type="text/javascript">
$(document).ready(function () {
// Tooltips
$('.tip').each(function () {
$(this).tooltip(
{
html: true,
title: $('#' + $(this).data('tip')).html()
});
});
});
</script>
This way you can create complex readable html content, and activate as many tooltips as you want.
live demo here on codepen
Just as normal, using data-original-title:
Html:
<div rel='tooltip' data-original-title='<h1>big tooltip</h1>'>Visible text</div>
Javascript:
$("[rel=tooltip]").tooltip({html:true});
The html parameter specifies how the tooltip text should be turned into DOM elements. By default Html code is escaped in tooltips to prevent XSS attacks. Say you display a username on your site and you show a small bio in a tooltip. If the html code isn't escaped and the user can edit the bio themselves they could inject malicious code.
The html data attribute does exactly what it says it does in the docs. Try this little example, no JavaScript necessary (broken into lines for clarification):
<span rel="tooltip"
data-toggle="tooltip"
data-html="true"
data-title="<table><tr><td style='color:red;'>complex</td><td>HTML</td></tr></table>"
>
hover over me to see HTML
</span>
JSFiddle demos:
Bootstrap 2.x
Bootstrap 3.x
set "html" option to true if you want to have html into tooltip. Actual html is determined by option "title" (link's title attribute shouldn't be set)
$('#example1').tooltip({placement: 'bottom', title: '<p class="testtooltip">par</p>', html: true});
Live sample
As of bootstrap 5, you can just specify the contents of the tooltip to be html DOM nodes when configuring it. Sample config...
// setup tools tips trigger
const tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
const tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
return new Tooltip(tooltipTriggerEl, {
html: true // <- this should do the trick!
})
});
For bootstrap 5, you can use data-bs-html="true" with the title tag data-bs-original-title="Tooltip Title"