10

Help me out, as I am new to jQuery & web development. My requirement was to ellipsize(...) the long text in text boxes and then if mouse is hovered over them display the full text in tooltip.

For this I used Bootstrap's tooltip . What I did was the following :

Downloaded bootstrap js and included in my file like this :

 <script type="text/javascript" src="scripts/bootstrap.min.js"></script>        

Changed my elemnt like this

<input type="text" name="tooltip" id="samplebox" data-toggle="tooltip" title="Ankit" >

For Dynamically updating the title, I made use of jQuery

<script>
    $(document).ready(function(){
        $(" #samplebox ").mouseenter(function(){
            var word = document.getElementById("samplebox").value;
            $(" #samplebox ").attr('title', word);
        });     
    });
</script>

This works fine for that one element.

now I have to do the same thing for more text-boxes, whose number can keep on increasing. Now, I want to create a generic function where I can pass the id of that element and get it done for any textbox over which mouse is hovered.

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
Ankit
  • 4,426
  • 7
  • 45
  • 62

5 Answers5

16

If there are no style classes, use the attribute-selector. For a tooltip, there's also the attribute value of data-toggle="tooltip":

$(document).ready(function(){
    $("[data-toggle=tooltip]").mouseenter(function () {
        var $this = $(this);
        $this.attr('title', $this.val());
    });   
});

Demo

Note: For the use with Bootstrap's tooltip you may have to change the attribute data-original-title instead if title to update the content of your tooltip. Have a look at this question

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
empiric
  • 7,825
  • 7
  • 37
  • 48
4

None of the codes above worked for my bootstrap tooltips. However, changing the data-original-title fixed the issue:

document.getElementById("samplebox").setAttribute("data-original-title", "newText");
Gimmly
  • 443
  • 1
  • 6
  • 15
3

You can use class selector like this

<input type="text" name="tooltip" class="samplebox" data-toggle="tooltip" title="Ankit" >

    <script>
    $(document).ready(function(){
        $(".samplebox").mouseenter(function(){
            var word = $(this).value();
            $(this).attr('title', word);
        });     
    });
</script>

This will fire the event on all the text boxes you dont need to create any generic function

Innam Hunzai
  • 442
  • 1
  • 6
  • 17
1
$(":input").mouseenter(function(){
   var val = $(this).val();
   $(this).attr('title', val);
});
areeb
  • 424
  • 3
  • 9
0

First add common class to every textbox.

<input type="text" name="tooltip" class="myclass" data-toggle="tooltip" title="Ankit" >

Then jQuery seems like this.

  <script>
    $(document).ready(function(){
        $(".myclass").mouseenter(function(){
            var word = document.getElementById("samplebox").value;
            $(this).attr('title', word);
        });     
    });
  </script>
Mayur Devmurari
  • 496
  • 9
  • 16