Sunday, November 30, 2008

[jQuery] Re: [Beginner alert] How do I output the selected part of the DOM in a web page in a JS alert()?

> The ultimate goal would be to filter the selection so that only text
> enclosed in given classes (say loremipsum and third in my example) are
> returned.

First off, I'm also new to jQuery so I apologize if my solution is the
best way to do this. Anyways, here's what I came up with

<html>
<head>
$(document).ready(function(){
$("a.lorem").click(function(){

alert($("p.lorem").eq(0).html()); // Alerts contents of first
"lorum" p tag
alert($("p.lorem").eq(1).html()); // Alerts contents of second
"lorum" p tag
return false; // Cancels link from being
followed
});
});
</head>
<body>
<a class="lorem" href="#">shoot</a>

<p class="lorem">Lorem ipsum dolor sit amet, magna massa aliquet
in
libero, a suscipit suspendisse ac penatibus, lectus donec
consequat, sed
justo
</p>
<p class="lorem"><span>Lorem ipsum dolor</span> <a href="#" >sit
amet,</a> <span>magna massa aliquet in</span>
libero, a suscipit suspendisse ac penatibus, lectus donec
consequat, sed
justo
</p>
</body>

No comments: