I have below HTML code ?
<label for="male">Hello This Will Have Some Value</label>
But actually i dont have enough space to show this much long label. So i thought of creating label as below..
<label for="male">Hello...</label>
Then i create a hidden field which will hold entire label value
<input type="hidden" name="Language" value="Hello This Will Have Some Value">
Now when user hovers mouse on Hello...
can i show hidden field's value Hello This Will Have Some Value
in tooltip using jquery?
Thanks!
This question is related to
javascript
jquery
css
html
If you also have jQuery UI, you can add this:
$(document).ready(function () {
$(document).tooltip();
});
You then need a title instead of a hidden input. RGraham already posted an answer doing this for you :P
You could use the title
attribute in html :)
<label title="This is the full title of the label">This is the...</label>
When you keep the mouse over for a brief moment, it should pop up with a box, containing the full title.
If you want more control, I suggest you look into the Tipsy Plugin for jQuery - It can be found at http://onehackoranother.com/projects/jquery/tipsy/ and is fairly simple to get started with.
Tooltips in bootstrap are good, but there is also a very good tooltip function in jQuery UI, which you can read about here.
Example:
<label for="male" id="foo" title="Hello This Will Have Some Value">Hello...</label>
Then, assuming you have already referenced jQuery and jQueryUI libraries in your head, add a script element like this:
<script type="text/javascript">
$(document).ready(function(){
$(this).tooltip();
});
</script>
This will display the contents of the title attribute as a tooltip when you rollover any element with a title attribute.
You don't have to use hidden field. Use "title" property. It will show browser default tooltip. You can then use jQuery plugin (like before mentioned bootstrap tooltip) to show custom formatted tooltip.
<label for="male" title="Hello This Will Have Some Value">Hello ...</label>
Hint: you can also use css to trim text, that does not fit into the box (text-overflow property). See http://jsfiddle.net/8eeHs/
You can use the css-property content
and attr
to display the content of an attribute in an :after
pseudo element. You could either use the default title attribute (which is a semantic solution), or create a custom attribute, e.g. data-title
.
HTML:
<label for="male" data-title="Please, refer to Wikipedia!">Male</label>
CSS:
label[data-title]{
position: relative;
&:hover:after{
font-size: 1rem;
font-weight: normal;
display: block;
position: absolute;
left: -8em;
bottom: 2em;
content: attr(data-title);
background-color: white;
width: 20em;
text-aling: center;
}
}
Just set a title on the label:
<label for="male" title="Hello This Will Have Some Value">Hello...</label>
Using jQuery:
<label for="male" data-title="Language" />
<input type="hidden" name="Language" value="Hello This Will Have Some Value">
$("label").prop("title", function() {
return $("input[name='" + $(this).data("title") + "']").text();
});
If you can use CSS3, you can use the text-overflow: ellipsis;
to handle the ellipsis for you so all you need to do is copy the text from the label into the title attribute using jQuery:
HTML:
<label for="male">Hello This Will Have Some Value</label>
CSS:
label {
display: inline-block;
width: 50px;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
jQuery:
$("label").prop("title", function() {
return $(this).text();
});
Example: http://jsfiddle.net/Xm8Xe/
Finally, if you need robust and cross-browser support, you could use the DotDotDot jQuery plugin.
Are you find with using standard tooltip? You could use title attribute like
<label for="male" title="Hello This Will Have Some Value">Hello...</label>
You could add the title attribute of same value to the element that label is for as well.
Source: Stackoverflow.com