Melih Rıfat Kaptan
Melih Rıfat Kaptan

Reputation: 43

My jquery method doesn't work

I really get confused . I have a javascript code that's posting to webmethod and getting string. And there is a h1 tag . I want to highlight the returning words at website (that's "abc" here). So my aspx code is below . My jquery method (highlight) doesn't work.Can anyone help with this ?

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" 
AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication4._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"></asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

<!doctype html>
<html lang="en">
<head>
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script src="http://www.google.com/jsapi" type="text/javascript"></script>
    <script type="text/javascript">
      google.load('visualization', '1.1', { packages: ['corechart', 'controls'] });
    </script>

    <script src="Scripts/jquery-1.11.3.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery-ui.min.js" type="text/javascript"></script>
</head>
<body>

<h1> abc sadaf dasfafa dsada abc afasgfa abc </h1>

<script>
    var exp;
    jQuery.fn.highlight = function (str, className) {
      var regex = new RegExp(str, "gi");
      return this.each(function () {
          $(this).contents().filter(function () {
              return this.nodeType == 3 && regex.test(this.nodeValue);
          }).replaceWith(function () {
              return (this.nodeValue || "").replace(regex, function (match) {
                return "<span class=\"" + className + "\">" + match + "</span>";
              });
          });
      });
    };

    $.ajax({
      type: 'POST',
      url: 'http://localhost:61216/Default.aspx/MyFunction',
      contentType: 'application/json; charset=utf-8',
      dataType: 'json',
      success: function (result) {
        exp = result.d;
      },
      error: function () {
        alert('Something occured');
      }
    });

    $(document).on('click', 'h1', function(){
      $('h1').highlight(exp, "highlight");
      alert("hi");
    });
</script>

</body>
</html>
</asp:Content>

Upvotes: 3

Views: 106

Answers (1)

Victor
Victor

Reputation: 5121

Your jquery function is working, as you can see below:

 var exp = 'abc';
 jQuery.fn.highlight = function(str, className) {
   var regex = new RegExp(str, "gi");
   return this.each(function() {
     $(this).contents().filter(function() {
       return this.nodeType == 3 && regex.test(this.nodeValue);
     }).replaceWith(function() {
       return (this.nodeValue || "").replace(regex, function(match) {
         return "<span class=" + className + ">" + match + "</span>";
       });
     });
   });
 };

 $(document).on('click', 'h1', function() {
   $('h1').highlight(exp, "highlight");
 });
.highlight {
  color: red;
  background-color: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<h1> abc sadaf dasfafa dsada abc afasgfa abc </h1>

The problem is the success callback of your ajax request. The highlight function is only being called when the user clicks on a h1 element. So when your ajax request ends you need to trigger the function to highlight the new exp. Try replacing the success callback with the code bellow and try again:

success: function (result) {
  exp = result.d;
  $('h1').highlight(exp, "highlight");
}

Upvotes: 1

Related Questions