Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys,

Is there any Jquery or any other way to display the title of the elements on giving focus just as the title displayed on hovering???

I have found this jquery but its not working.....Can anyone help please... Thanks in Advance!!!!!!!!!!
C#
$(function () {
        var xOffset = 20;
        var yOffset = 30;
    $('input').focus(function (e) {
        this.t = this.title;
        this.title = "";
        $("body").append("<span id='tooltip'>" + this.t + "</span>");
        $("#tooltip").css("top", (e.pageY - xOffset) + "px").css("left", (e.pageX + yOffset) + "px").fadeIn("fast");
    });

    $('input').blur(function (e) {
        this.title = this.t;
        $("#tooltip").remove();

        $("#tooltip").css("top", (e.pageY - xOffset) + "px").css("left", (e.pageX + yOffset) + "px");
    });

});


HTML code is as follows-
XML
<body id="loginbody" class="login-body">

    <input type="hidden" id="page-culture-value" value="en" />

        <div class="login-tr">
        <div class="login-td">

            <div class="login-page"  >
                <div class="login-block">
                    

                    <form id="main" action="?" method="post">
                        <div class="form-item">
                            <label for="email" tabindex="1">Username</label>
                            <input type="text" tabindex="1" title="Enter Email here" id="email" name="email" class="form-text with-text">
                        </div>

                        <div class="form-item">
                            <label for="password" tabindex="30">Password</label>
                            <input type="password" tabindex="30" title="Enter Password here" id="password" name="password" class="form-text" value="">
                        </div>

                        <div class="check-label">
                         <input title="Click to Stay Signed In" tabindex="40" type="checkbox" value="1" name="remember" id="remember" >
                            <label for="remember">Stay signed in</label>
                        </div>

                                <input type="hidden" name="rurl" value="" />
                            <input title="Click to Submit" tabindex="60" type="submit" value="Sign in" class="blue-button">
</form>
                    <div class="links">




                        <p><a href="#" id="reset-password" tabindex="70" style="color: #000000;" title="Pop-Up window for resetting Password">I forgot my password</a></p>
                    </div>

                        <a href="?lang=fr"  class="grey-button button-fr" title="Press to select French" tabindex="75"><span>FRANÇAIS</span></a>

                </div>
                <div class="foot">&copy; /div>

            </div>

        </div>


    </div>
    <div class="popup-form popup-win" id="popup-reset-password">
        <h1 id="resetpassheading">
            Reset password
        </h1>
        <form id="reset-password-form" action="/Home/ResetPassword" method="post" class="modules-form" enctype="multipart/form-data">

            <div class="form-item">
                <label for="email-reset">Username</label>
                <input title="Enter your Email " tabindex="8" type="text" id="email-reset" name="email-reset" class="form-text with-text" placeholder="Email Address" data-required="true">
            </div>

            <div class="buttons">
                <input title=" Click for OK" tabindex="9" type="submit" class="blue-button ok-button" value="Ok">
                <input title="Click for Cancel" tabindex="10" type="button" class="blue-button cancel-button" value="Cancel"...<>
            </div>
        </form>



</div>
</body>
Posted
Updated 20-Nov-14 7:55am
v2
Comments
Sergey Alexandrovich Kryukov 20-Nov-14 13:00pm    
This is not a big problem, but you should write the query by yourself, according to your design, using other queries just for the source of ideas. Where 1) do you want to display the title, 2) is the source of data for your title?
—SA
Sergey Alexandrovich Kryukov 20-Nov-14 13:09pm    
Next time, showing a script sample, always show the HTML sample the script works with. It would be the best to show self-containing HTML with script embedded in it, and the CSS, if needed. Without HTML, this kind of script samples makes little sense.
Please see: SSCCE.
—SA
Afzaal Ahmad Zeeshan 20-Nov-14 13:23pm    
I don't believe that this code would work with any HTML either, as far as I know there is no this.t and I believe this won't work since this is a JavaScript object not a jQuery object to work with.

This question pretty much is a copy/paste problem. I will add it to my list of "Problems initialized by a copy/paste code". :D
Sergey Alexandrovich Kryukov 20-Nov-14 13:36pm    
Agree. I did not pay much attention for this code, which I called "who-knows-what". But I think my recommendation two show HTML sample is quite appropriate and valid in all cases.
Good point about "copy/paste code". :-)
—SA
Afzaal Ahmad Zeeshan 20-Nov-14 13:49pm    
.. your recommendation is always valid and would be fair to ask for enough information to proceed and provide a good answer. Otherwise I, you, another user would all just provide vague answers depending on our belief and considerations of the errors in the JavaScript-only code.

Please see my comment to the question. Don't just copy a script doing who-knows-what. Create your ones from first principles, which are very simple.

Don't mix up focusing and hovering. Not only they are different events, they are actually unrelated.

This is how you can handle hovering: http://api.jquery.com/hover/[^].
Note that the functions accepts two handlers: for mouse moving over the control and out of it.

This is how to handle focusing and blurring (when the keyboard focus is grabbed by other control):
http://api.jquery.com/focus[^],
http://api.jquery.com/blur[^].

In the event handlers, you can add/remove some content in any element, or you can change CSS style to hide show the title content:
http://api.jquery.com/category/manipulation[^],
http://api.jquery.com/category/css[^].

Combining all these methods at your liking, you can create any effects on focusing/blurring and any effects on hovering.

—SA
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 20-Nov-14 13:24pm    
This is enough for such a question, since he doesn't even know where he was stuck and why he was stuck. +5 by my side. :-)
Sergey Alexandrovich Kryukov 20-Nov-14 13:29pm    
Thank you, Afzaal.
—SA
ssharma21 20-Nov-14 13:59pm    
Thanks Sergey for your solution. I am new in Jquery so didnt relate my HTML code after seeing that Jquery code. I have provided my HTML page coding as well...Can you help me further by just proving a general jquery that will work with all elements in my page displaying their titles when received focus
Sergey Alexandrovich Kryukov 20-Nov-14 15:58pm    
How about just the idea? Use jQuery .each() to add the same handler to all controls of some set: http://api.jquery.com/jquery.each
You did not yet answered: where do you store the titles of all the controls ("title" attribute? it's not standard...)?
—SA
Title being displayed on hovering is the default function as the title is to just explain or describe the actual purpose of the element to the user. For example hover over to the following paragraph.

Hover over me.



You can write your own functions for handling the events in web pages. To handle the focus event you would write this code,

JavaScript
$('selector').focus(function () {
   // code to execute
});


.. this would trigger each time focus is on the selected element. Let us work out for the title, for example, you wanted to show the title attribute property of the element upon focus on the screen, you would write this code,

JavaScript
$('selector').focus(function () {
   // get the title attribute, and write it in the paragraph
   $('p').text($(this).attr('title')));
});


You should first and foremost read the Solution 1 because Sergey has provided the reference links to the resource web pages you should read, I am going to add a little information to it. First thing is, you cannot trigger that "Show title" function of the browser anyway, not by focusing not by hovering over it. That is; I don't know how and why, the browser's behaviour. Secondly, you would use that

JavaScript
$('body').append('<span class="title">Hello World!</span>');


.. function to create a new element, and add the content to it and add a few styles to it to make it float over other elements. You would create a time interval to remove the element you just created, for example think of the setTimeout,

JavaScript
setInterval(removeTitle, 5000); // 5 seconds

function removeTitle () {
   $('span.title').remove();
}


One last thing I am going to add to the Solution 1 is that Sergey has said to write your own fiddles. Yup! That is true you should always write your own or just get an idea of code from other people don't copy/paste it. What I think, is the problem with this code, is that I don't think there is any t property in your context which would have that title attribute's value. That is the actual trouble in your code.

JavaScript
// you're appending the value to the t property, which doesn't even exist
this.t = this.title;
// then removing the title property
this.title = "";


Tip: When working with jQuery code, always use the jQuery object ($(this)) and not the JavaScript object (this). It will also prove to be helpfull while working with objects.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 20-Nov-14 13:33pm    
Voted 4. You misspelled some words, at least "selector" at first sample. It's not the best idea to advise alert, as soon as other way of presentation (append) is shown (but then, why not showing how to remove the content?)...
—SA
Afzaal Ahmad Zeeshan 20-Nov-14 13:45pm    
I am really sorry Sergey, I have removed that typo from my answer I was really confused while spelling it :D

I have also, removed the alert. I, myself, was having a bad feeling like writing that alert function that it is not a UX but I don't know why I did, and now I have changed that to a simple text update for a paragraph.

I have added a function that would remove the span from the HTML DOM after 5 seconds. I am not sure how would it behave if there is a new focus event before 5 seconds ;-)

How does it look like now? :-)
Sergey Alexandrovich Kryukov 20-Nov-14 13:55pm    
I guess, remove is best on hover (second handler, going out) or blur. The problem with 5 sec is this: consider you are working with focus: put one title on focus of one control, then focus another control, and the timer from previous even will eventually remove the content. You can handle it to wind up the timer again, but why?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900