views:

92

answers:

1

I need a little piece of advice. I have a test page with 2 fields: word number and URL Also i have a button Push.
When i push the button i want to open the specified URL (it's local html files) and highlight the word at the "word number" position Of course the code must ignore element nodes (<p>,<b>,<table> and so on)

A: 
$('#button_id').bind('click', function(e){
   var $wordat  = $('#input_id_word').val(),
       $url     = $('#input_id_url').val(),
       counter  = 0;

   $.ajax({
      type: 'GET',
      url: $url,
      dataType: 'html',
      success:  function(data){
          $(data).find('body').contents().each(function(i, e){
              var wordarr = $(e).text().split(" ");

              counter += wordarr.length;

              if($wordat <= counter){
                 wordarr[$wordat] = "<span style='background-color: red;'>" + wordarr[$wordat] + "</span>";                     
              }

              $(e).text(wordarr.join(" "));
          });

          $('element_id_to_insert').html(data);
      }
   });
});

This is untested and can get improved like a lot, but something very similar to this should do it.

jAndy
@Felix. Exactly this i want to do. If i have <p>This is <b><u>TEST</u></b> the word that is highlighted will be TEST (for number = 3).@jAndy. i got a js error (in FF error console) s.url is undefined ... error is in jquery.jsThanks
tinti
I made a little research. First of all i have a old version of jquery.js. I update it at newest version. Now i don't have any errors in console but nothing it's displayed. Thanks
tinti
nobody write here for almost 1 day.i discover the problems from the script above1. $(data).find('body').contents().each(function(i, e) - doesn't work2. wordarr[$wordat] = "<span style='background-color: red;'>" + wordarr[$wordat] + "</span>"; this is correct but jquery doesn't take this as a html codeIf anyone can help me with this 2 problem i will be very happy :)Thanks
tinti