user3641206
user3641206

Reputation: 3

Selecting text with javascript/jquery

I have a server query, that queries a minecraft server and displays its status.It stores the ip and the server name in a variable and displays it with jquery in a single loop.

I want the dispalyed server name to be selectable onclick, but whatever i try it doesnt work

the code

$('.servercontainer .serverdata span[rel=\"'+serverarray[i]+'\"]').html(servername[i]);

here servername is the variable which contains the servername, server ip contains the ip and servercontainer is the class of the div that displays all the data.

I want to be able to select the server name when clicked on.

Thanks for reading this
edit: the servername is what i want selected. When a person clicks on the text, the whole text has to be highlighted so that all he has to do is ctrl+c to copy it.

Sorry for the confusion

Upvotes: 0

Views: 83

Answers (3)

miny1997
miny1997

Reputation: 478

Do you mean this:

<SCRIPT>$(document).ready(
    function() {
        $('.copyText').click(
            function() {
                if ($('#tmp').length) {
                    $('#tmp').remove();
                }
                var clickText = $(this).text();
                $('<textarea id="tmp" />')
                    .appendTo($(this))
                    .val(clickText)
                    .focus()
                    .select();
        return false;
    });
$(':not(.copyText)').click(
    function(){
        $('#tmp').remove();
    });

});
</SCRIPT>
<SPAN CLASS="copyText">Click Me!</SPAN>
<SPAN>Not Me!</SPAN>

Live Demo at http://jsfiddle.net/Wx4YN/1/

If yes, don't forget that this need jQuery

Have some little bugs when you use div or p.

Upvotes: 0

pumpkinzzz
pumpkinzzz

Reputation: 2967

Not sure to understand.. If you mean that u want the servername to be clickable and linking to the server IP, you can do:

$('.servercontainer .serverdata span[rel=\"'+serverarray[i]+'\"]').html("<a href='"+serverip[i]+"'>"+servername[i]+"</a>");

Upvotes: 1

Foo
Foo

Reputation: 158

Hard to answer without more context, but assuming you're just trying to grab the text value of the clicked item, try using the .text() method...

example:

$('.servercontainer .serverdata span[rel=\"'+serverarray[i]+'\"]').click( function () {
  var capturedText = $(this).text();
  // do something with the captured text...
});

... instead of .html() if you just want the text.

Upvotes: 0

Related Questions