3

Is there a way to capture word value with jquery or javascript? in the example :

Search: Quotes of the Month for May

When I click "Search:" or "Quotes" or any word, that I alert that word text?

Update :

This is what I meant :

http://jsfiddle.net/BE68L/

Is there a way I can get text value without wrapping it into some html element?

London
  • 14,986
  • 35
  • 106
  • 147
  • 3
    Your question doesn't make sense. A code sample illustrating your goal would definitely make this clearer. Also you could use http://jsfiddle.net to play around and post snippets of HTML/CSS/javascript. – Darin Dimitrov May 30 '11 at 21:47
  • Double-click a word to alert the text of the word that you clicked? – David Thomas May 30 '11 at 21:48
  • Similar to http://stackoverflow.com/questions/396649/retrieving-dom-textnode-position ? – Niklas May 30 '11 at 21:49
  • @Niklas, it doesn't seem to be that similar. This question seems to want to retrieve the selected word, that question seems to want to find out where that word/text is on the page. – David Thomas May 30 '11 at 21:50
  • @David, except finding the position of the texts on the page would be the only way to actually find which words are clicked in paragraphs etc. as you would have to look inside TextRange objects and specifically map the click coordinates to every single word inside an element, to find accurately the word that was clicked. – Niklas May 30 '11 at 21:52
  • @Niklas: there are similarities, certainly, but the *focus* of the questions appear sufficiently different to not be duplicates. – David Thomas May 30 '11 at 21:53
  • @David, I'll settle with that :), but to solve this question, I do believe that previous question touches on the only way to find the correct word in paragraphs. – Niklas May 30 '11 at 21:56
  • @London Have a look at my updated answer. It is a relatively straight forward solution for non IE browsers which does not require any element wrapping or such. – Niklas Jun 04 '11 at 22:58
  • are you trying to find the value of the input box ? `.val()` will do that http://api.jquery.com/val/ try this too http://api.jquery.com/text/ – zod May 30 '11 at 21:54

2 Answers2

1

To find specific words in longer paragraphs you would have to create Range objects and TextRange's to find the coordinates of specific words in the paragraph. Once you have mapped the coordinates for each word, you could then match the window click event coordinates to the word that is within the range.

The question at Retrieving DOM textnode position has some similarities of what you are after.

In short, I think it should be perfectly doable to accurately get the word that is clicked, even in longer paragraphs, but it certainly isn't very easy, and for longer pieces of texts, it may end up being very inefficient.

edit for mozilla/webkit browsers, you could do it like this:

$(document).ready(function() {
    $("body").click(function(){
         var s = window.getSelection();
        s.modify('extend','backward','word');        
        var b = s.toString();

        s.modify('extend','forward','word');
        var a = s.toString();
        s.modify('move','forward','character');
         alert(b+a);

    });
});

example: http://jsfiddle.net/niklasvh/rD2uE/

It doesn't work on IE <= 8 though, you'd probably need to do a different kind of solution using the IE selections.

Community
  • 1
  • 1
Niklas
  • 29,752
  • 5
  • 50
  • 71
1

You cannot do it without wrapping it in HTML nodes. You can apply some Javascript magic to an "unwrapped text", which basically means a Textnode. This could look like:

$('#foo').contents().each(function(_, node) {
    var nodelist = node.textContent.split(/\s/).map(function( word ) {
        return $('<span>', {
            text: word + ' ',
            click: function() {
                alert( $(this).text() );
            }
        }).get(0);
    });

    $('#foo').empty().append(nodelist);
});

demo: http://jsfiddle.net/LFCWp/

I just wrote that down. There is still a ton of optimize potential, but it should give you a basic idea of how to accomplish it.

jAndy
  • 231,737
  • 57
  • 305
  • 359
  • You **can** do it without wrapping it in HTML nodes. Check my answer. – Niklas May 30 '11 at 22:23
  • @jAndy nothing renders in my page, I see it in the source code though. If I remove `$('#foo').empty().append(nodelist); ` click thing doesn't work, maybe because I have
    elements
    – London May 30 '11 at 22:42
  • @Niklas: well yay, I probably was a little quick on my first sentence there. @London: which browser are you actually using? My code uses an es5 feature, so a "new'ish" browser is needed. – jAndy May 30 '11 at 22:52
  • @jAndy thanx for the response, I use ff & chrome latest probably, yep definitely
    or
    screws things up
    – London May 30 '11 at 22:57
  • @London: I wouldn't know how `
    ` tags should screw up anything on the above code.
    – jAndy May 30 '11 at 23:00
  • @jAndy no need for the sarcasm. I simply stated that your argument simply wasn't true. If you weren't certain that there wasn't any other way, you could have just left the statement out and provided your solution, which works great in certain situations. – Niklas May 31 '11 at 08:10