Tooltip css class lost after web service call via jquery

ibrahim

I have a jquery web service that affects css class of tooltip on the same page.

The problem is this; when the page initially loads, tooltip css works fine, but after web service call vith a button on page, tooltip class lost and it is shown as standard view. What will cause this?

c# Code Block

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SampleWeb {
  public partial class Sample: System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) {
      if (!IsPostBack) {
        GetDataForPageLoad();
      }
    }

    public void GetDataForPageLoad() {
      StringBuilder sb = new StringBuilder();

      lblData.Text = string.Format("<a title=\"click to view topic {0}.\" class=\"masterTooltip\"><img width=\"50px\" src='images/Chrysanthemum.jpg' alt=\"{0}\"  /></a><br/>", "XXX");
    }

    [WebMethod]
    public static string GetData() {
      return string.Format("<a title=\"click to view topic {0}.\" class=\"masterTooltip\"><img width=\"50px\" src='images/Chrysanthemum.jpg' alt=\"{0}\"  /></a><br/>", "XXX");
    }
  }
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Sample.aspx.cs" Inherits="SampleWeb.Sample" %>

  <!DOCTYPE html>

  <html xmlns="http://www.w3.org/1999/xhtml">

  <head runat="server">

    <title>test</title>
    <style type="text/css">
      .tooltip {
        display: none;
        position: absolute;
        border: 1px solid #b83e3e;
        background-color: #d84949;
        border-radius: 5px;
        padding: 5px;
        color: #fff;
      }
    </style>


    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>


    <script>
      function GetData() {

        $.ajax({
          url: 'http://localhost:6878/Sample.aspx/GetData',
          type: 'POST',
          data: '',
          dataType: 'json',
          contentType: 'application/json',
          success: function(data) {

            var obj = document.getElementById("lblData");
            obj.innerHTML = obj.innerHTML + data.d;
          }
        });
      }
    </script>
  </head>

  <body>
    <form id="form1" runat="server">
      <input id="Button1" type="button" value="Get" onclick="GetData();" />
      <br />
      <br />
      <asp:Label ID="lblData" runat="server" Text=""></asp:Label>



      <script type="text/javascript">
        $(document).ready(function() {
          // Tooltip only Text
          $('.masterTooltip').hover(function() {
            // Hover over code
            var title = $(this).attr('title');
            $(this).data('tipText', title).removeAttr('title');
            $('<p class="tooltip"></p>')
              .text(title)
              .appendTo('body')
              .fadeIn('slow');
          }, function() {
            // Hover out code
            $(this).attr('title', $(this).data('tipText'));
            $('.tooltip').remove();
          }).mousemove(function(e) {
            var mousex = e.pageX + 20; //Get X coordinates
            var mousey = e.pageY + 10; //Get Y coordinates
            $('.tooltip')
              .css({
                top: mousey,
                left: mousex
              })
          });
        });
      </script>



    </form>
  </body>

  </html>

machineghost

I'm not certain, but I suspect this line is your problem:

obj.innerHTML = obj.innerHTML + data.d;

You hook up your tooltips with this line:

$('.masterTooltip').hover(function() {

which hooks up a hover handler on all .masterTooltip elements on the page ... at that moment (with "that moment" being when the page loads and triggers the DOM ready event).

But when you mess with the innerHTML of an element you are effectively removing the old elements and replacing them with new ones, and since you haven't hooked up a hover handler to those new ones, they don't work.

If I'm correct I see two possible solutions:

1) Put all that $('.masterTooltip').hover(function() { code inside a function. Call that function onReady, as you currently are, but then call it again after the innerHTML change to hook up the handler to the new elements

2) Don't remove the old elements at all; instead of using innerHTML to replace the old lblData contents:

var obj = document.getElementById("lblData");
obj.innerHTML = obj.innerHTML + data.d;

try using jQuery to just add a new element:

$('#lblData').append('<span>' + data.id + '</span>');
// I'm not even sure the span is needed, but I added it to be safe

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Http request and response lost after receiving Response from Web Service?

From Dev

How to call an AXIS web service via AJAX?

From Dev

jQuery on click event is lost after DOM updated by ajax call

From Dev

JQuery button is not triggering service call via ajax

From Dev

Using JSON to Call Web Service from jQuery

From Dev

Jquery Ajax Fail to call web service

From Dev

AngularJS Select Value Being Lost After Call to Restful Service and Scope Object Update

From Dev

Call web service running on Parallels from iPhone connected via USB

From Dev

Adding tooltip to Bootstrap via JQuery

From Dev

JPA - How to call method for web service after commit

From Dev

How do I call a method in a web service class once a day?

From Dev

Is there class/library/framework to call web service in SWIFT language in iOS?

From Dev

Is there class/library/framework to call web service in SWIFT language in iOS?

From Dev

Change "div class" of jQuery tooltip

From Dev

Calling PHP Web service via ajax in jQuery mobile

From Dev

jQuery Ajax POST call with JSON in Rest Web Service Issue

From Dev

Call web2py jsonrpc service using jquery

From Dev

Service Stack Session Lost After File Upload

From Dev

Populate jQuery UI accordion after AngularJS service call

From Dev

Call CSS Class instead of inserting Attribute using JQuery (.css())

From Dev

Call CSS Class instead of inserting Attribute using JQuery (.css())

From Dev

Web Service Call Return

From Dev

Finding an Image's Width After Adding a CSS Class via Javascript

From Dev

jquery UI tooltip - what 'my' and 'at' mean? Why call tooltip() twice?

From Dev

how to use tooltip in wordpress loop via jquery

From Dev

Node reference is lost after modifying a class name

From Dev

Reliable Messaging accessible after adding ASMX web service via Service Reference

From Dev

jQuery update li class attribute href after ajax call

From Dev

jQuery update li class attribute href after ajax call

Related Related

  1. 1

    Http request and response lost after receiving Response from Web Service?

  2. 2

    How to call an AXIS web service via AJAX?

  3. 3

    jQuery on click event is lost after DOM updated by ajax call

  4. 4

    JQuery button is not triggering service call via ajax

  5. 5

    Using JSON to Call Web Service from jQuery

  6. 6

    Jquery Ajax Fail to call web service

  7. 7

    AngularJS Select Value Being Lost After Call to Restful Service and Scope Object Update

  8. 8

    Call web service running on Parallels from iPhone connected via USB

  9. 9

    Adding tooltip to Bootstrap via JQuery

  10. 10

    JPA - How to call method for web service after commit

  11. 11

    How do I call a method in a web service class once a day?

  12. 12

    Is there class/library/framework to call web service in SWIFT language in iOS?

  13. 13

    Is there class/library/framework to call web service in SWIFT language in iOS?

  14. 14

    Change "div class" of jQuery tooltip

  15. 15

    Calling PHP Web service via ajax in jQuery mobile

  16. 16

    jQuery Ajax POST call with JSON in Rest Web Service Issue

  17. 17

    Call web2py jsonrpc service using jquery

  18. 18

    Service Stack Session Lost After File Upload

  19. 19

    Populate jQuery UI accordion after AngularJS service call

  20. 20

    Call CSS Class instead of inserting Attribute using JQuery (.css())

  21. 21

    Call CSS Class instead of inserting Attribute using JQuery (.css())

  22. 22

    Web Service Call Return

  23. 23

    Finding an Image's Width After Adding a CSS Class via Javascript

  24. 24

    jquery UI tooltip - what 'my' and 'at' mean? Why call tooltip() twice?

  25. 25

    how to use tooltip in wordpress loop via jquery

  26. 26

    Node reference is lost after modifying a class name

  27. 27

    Reliable Messaging accessible after adding ASMX web service via Service Reference

  28. 28

    jQuery update li class attribute href after ajax call

  29. 29

    jQuery update li class attribute href after ajax call

HotTag

Archive