3

I have unordered list of links and i am trying to get the clicked link text. So when i click on some link i would like to display in paragraph or textbox at the bottom of my list text that is cliked. So if I have something like this:

  1. item1
  2. item2
  3. item3

If i click on item2 i would like to get it like: "You just clicked:item2 "

And i manage that with this:

jQuery(function () {
    $('a').click(function () {
        alert('Text is: ' + $(this).text());
    });
});

But that is displaying an alert message. then i do this:

jQuery(function () {
    $('a').click(function () {
        var name = $(this).text();
        $("p#selector").text(name); 
        $("input#textbox").val(name);
    });
});

And it works it send text value of a link to paragraph but it disappear really fast, it show it about second and it's gone, is there any way to prevent this? To stop it from disappearing?

Rob Cowie
  • 22,259
  • 6
  • 62
  • 56
Goran Bralo
  • 453
  • 3
  • 11
  • 23

2 Answers2

3

Try this:

jQuery(function () {
    $('a').click(function (e) {
        e.preventDefault();
        var name = $(this).text();
        $("#selector").text(name); 
        $("#textbox").val(name);
    });
});

e.preventDefault() will prevent the link from doing whatever it is doing by default (which sounds like it could be refreshing the page...).

Here's a demo.

I've also amended your selectors - p#selector is inefficient, you should simply use #selector when selecting by ID, as documented in the jQuery API.

For id selectors, jQuery uses the JavaScript function document.getElementById(), which is extremely efficient. When another selector is attached to the id selector, such as h2#pageTitle, jQuery performs an additional check before identifying the element as a match.

EDIT: As it's become apparent that the click handler isn't what you need here, try this solution:

Parse the URL to get the current page using a jQuery URL Parser, and then find the link that corresponds to the URL and get the text:

var url = "one.htm";
var linktext = $("a[href='" + url + "']").text();
$('#output').text(linktext);

Working demo of that bit (just do the URL parsing instead of setting the URL manually).

Town
  • 14,706
  • 3
  • 48
  • 72
  • yea that is allright but it prevents my link to work. is there any way to fix that? I mean i need that link to work i have that list on 5-6 pages and that links are important to me. – Goran Bralo May 11 '11 at 10:37
  • @user748393: Surely you don't want your link to work as a link, isn't that the point? – Town May 11 '11 at 10:39
  • @GoranB: 'work' in the sense that it loads a new page? Surely if that happens, you won't see the results of your click handler? – Rob Cowie May 11 '11 at 10:42
  • @Rob yea it should load a new page, i am not so good with jquery i am working on asp.net mvc3 project so i thought it would be cool to use jquery feature, but it seems that i will have to make some workaround – Goran Bralo May 11 '11 at 10:46
  • @GoranB: Like @Rob Cowie says, if you navigate to a different page then the page containing the paragraph won't be there anymore... Maybe update your question with some HTML and a wider description. – Town May 11 '11 at 10:50
  • @GoranB: Your use of jquery is fine, and jquery is doing what you are asking it to. The problem - if there is one - is that you are using jquery to update a page _and_ expecting the link to load a _new_ page. If you do that, then the changes you just made (i.e. adding text to a paragraph) will disappear almost immediately. – Rob Cowie May 11 '11 at 11:04
  • @GoranB: @Rob Cowie: I think this could be handled by parsing the URL to get the active page on first load, finding the corresponding link and then displaying the text in the `p` tag. That way it would show the 'clicked' link (ie: the current page). – Town May 11 '11 at 11:09
  • do you have any example of that? it would be cool to take the last 3 digits from url becouse in my case it's id andstore it in variable to use on that page – Goran Bralo May 11 '11 at 11:20
  • I think i have found myself another solution my url looks like: http://localhost:7264/Asortiman/Browse?kategorije=327 so is it possible to catch last 3 digits in this case 327 that would save me. i wolud take that number and use it as param to another function – Goran Bralo May 11 '11 at 11:52
  • @Goran B: Use something like [this](http://stackoverflow.com/questions/979975/how-to-get-the-value-from-url-parameter). – Town May 11 '11 at 11:57
1

Try preventing the click event from propagating after you handle it by returning false from the function.

jQuery(function() {
    $('a').click(function () {
        var name = $(this).text();
        $("p#selector").text(name); 
        $("input#textbox").val(name);
        return false;
    });
});

EDIT 1: Which is functionally identical to the answer provided by @Town, who beat me to it

EDIT 2: return false is not quite identical to .preventDefault() (which prevents the default event from occurring, but does not prevent other registered handlers from firing) or indeed .stopPropagation() (which stops event 'bubbling' and prevents handlers further up the DOM from firing). Returning false causes both but as @Town says, if the handler errors before returning, the default event will occur.

Basically... do what he said.

Rob Cowie
  • 22,259
  • 6
  • 62
  • 56
  • @Rob Cowie: `e.preventDefault()` will always be called, whereas `return false` will not be reached in the event of an error in the function. `return false` also [prevents all propagation](http://stackoverflow.com/questions/2017755/whats-the-difference-between-e-preventdefault-and-return-false) (which may or may not be desired). So not quite functionally identical ;) – Town May 11 '11 at 10:18
  • @Rob Cowie: Haha! And you win the points! Not my day, obviously :p – Town May 11 '11 at 10:35
  • @Town: Sheesh... I even mentioned your answer in my edit. Perhaps I should have deleted my post. Sorry 'bout that old boy. – Rob Cowie May 11 '11 at 10:40
  • @Rob Cowie: No problem! Your answer is different so deleting it would have devalued this question. Just bask in the glory mate :) – Town May 11 '11 at 10:41
  • @Town: There we go... now I got nothin! Entertaining stuff... beats working. – Rob Cowie May 11 '11 at 10:43
  • @Rob Cowie: I literally have no idea what's going on anymore! – Town May 11 '11 at 10:51