views:

184

answers:

4

i want to have a link and have it call a javascript function but i want to pass the text of the link into the function.

i am trying to create a dialog that displays the name on the original link.

would jquery be helpful here?

+1  A: 

Not sure exactly if this is what you are looking for but:

<a href="#" id="mylink">Some Text here</a>

$('#mylink').click(function(){ myfunc($(this).text()); return false; });
Mark
what does this code do? i am trying to have the text in the link show up on a confirmation dialog
ooo
it is just a rough example, but basically it is taking the text inside of the <a> link, and passing it to myfunc() as an argument. myfunc is just an example function name.
Mark
+1  A: 

jQuery UI has a dialog function which would make it easy.

I'd create a hidden div:

<!-- Temporary elements --> 
<!-- ui-dialog --> 
<div id="dialog" title=" "> 
</div>

And in $(document).ready add:

jQuery('#dialog').dialog({
     autoOpen: false,
     modal: true,
     width: 625, 
     position: 'center'
}); /* end #dialog */

Then, in the click event of the link, set the title and text as:

jQuery('.ui-dialog-title').text(/* yourtext */);
jQuery('.ui-dialog-content').html(/* link name or whatever */);

jQuery('#dialog').dialog('open');
return false;

Those classes are automatically added by the dialog.

edit: forgot to mention, you'll want to open the dialog in the same click event and return false so the original link href doesn't execute.

Jim Schubert
how do you put all this code in the click event of the link?
ooo
go to my online portfolio: http://info300.net/jrschubert/ and view source. I do exactly this to display JSON data in a dialog. in a click event.
Jim Schubert
A: 

Expanding on the previous answer, if you just want a dialog box it should be (jquery required):

<a href="#" id="mylink">Some Text here</a>

$('#mylink').click(function(){ alert($(this).text()); return false; });
Swish
where do you put the following code?$('#mylink').click(function(){ alert($(this).text()); return false; });anywhere in the page?
ooo
if you just want a dialog alert, you can put in the link's onclick event: "function(){ alert($(this).text()); return false;}"
Jim Schubert
+1  A: 

A non jQuery way of doing this is to just assign a simple onclick handler to the link

<html>
<head>

    <script>
         function foo(link)
            {
                alert(link.innerHTML);
                return false;
            }
    </script>
</head>
<body>
    <a href="#" onclick="foo(this);">blah</a>
</body>
</html>
Logan