1

How would I create a div that when clicked sends an ajax call to a jsp page to replace the content of the div that has been clicked while adding a css class to the changed div?

2 Answers 2

4
<div id="foo"></div>

and then:

$('#foo').live('click', function() {
    $(this).load('/bar.jsp', function() {
        $('#foo').addClass('foobar');
    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

I'd suggest not to use self closing tags, as IE 7 & 6 do not support those; so please use the closing tag </div>
never heard of live i prefer sticking to the onClick in the div tag like @nayish suggested, thanks anyways...
@Muki, the .live method allows you to attach an event handler to a DOM element which would persist even if you replace the contents of this element.
@Muki I'd suggest that you go with this method, as the live method solves the purpose that you are looking for.
0

the div in the view:

<div id="a" class="a" onclick="clickFunc()">...</div>

the javascript function:

function clickFunc() {$("#a").load("whatever.jsp"); $("#a").addClass("newClass");}

Hope this is what your looking for.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.