views:

66

answers:

3

In my page there are two 'Conatct Selected' anchor button, one at the top having id as "selectAllLink" and other at the bottom having id as "selectAllLinkB" of the page. The top one working perfectly using the following script:

<script type="text/javascript">

$(function() {
    $('#selectAllLink').each(function() {
        var a = [];
        var n = $("td.title_listing input:checked");
        var s = "";

        n.each(function() {
            a.push(this.value);
        });
        s = a.join(',');

        if (a.length > 0)
            this.href = "/D_ContactSeller.aspx?property=" + s;
        else
            this.href = 'javascript:alert("Select at least one property to contact!");';
        return false;
    });
});

now for my bottom anchor button i tried to put it's id as 'selectAllLinkB' and changed the code like this:

    <script type="text/javascript">

    $(function() {
        $('#selectAllLink, selectAllLinkB').each(function() {
            var a = [];
            var n = $("td.title_listing input:checked");
            var s = "";
           .....
           .....
           .....
    });

Is there any simple method to call the script for both the anchor button event? waiting for the fast and good responce. Thanks in Advance...

+4  A: 

You are quite close. The selector should be '#selectAllLink, #selectAllLinkB'.

However, you might want to use the click method instead of each to register a click event instead of setting the href attributes at load time. Now you are getting the checkboxes that are checked when the page loads, not when the link is clicked.

Guffa
Well that's working Perfectly...Thanks.But i still have a problem. When there is no checkbox is selected it gives javascript message. And after clicking 'ok' it starts loading a false Iframe page over the page. And on single click of mouse it goes out.Do you have any idea how can i Remove this problem?Thanks in advance..
Sanju
@Sanju: It seems like you have some other code that loads the Iframe. Look at Justin's code for an example on how to accept the event object as parameter and use the preventDefault method to stop the click so that it doesn't trigger anything else.
Guffa
A: 

I'd change from the id to a class and so something like:

<script type="text/javascript"> 

$(function() { 
    $('.selectAllLink').each(function() { ...
    }
}
Paulo Santos
Why? I would have thought using the id selector would be quicker as it will use the getElementById function and not have to traverse the DOM to find the element
Naeem Sarfraz
+1 It's more performant to specify ID's yes, but when you begin to define the same functionality for more than one element, it's cleaner to create a class to identify that element collection by. This also allows you to add any number of elements to the collection simply by applying the class *without* having to change the JavaScript (which is sometimes a requirement depending on a development team). So the trade off is readability and scalability/ease-of-use compared to a (likely negligible) performance increase. A benchmark would be interesting to see.
Justin Johnson
+1  A: 

You've gone through the trouble of using unobtrusive JavaScript to modify the href, and you even have jQuery. There's really no reason to be using this method

this.href = 'javascript:alert("Select at least one property to contact!");';

Instead, you should consider using a more modern event registration method:

$(function() {
    $('#selectAllLink, #selectAllLinkB').click(function(e) {
        var a = [];
        $("td.title_listing input:checked").each(function() {
            a.push(this.value);
        });

        if ( a.length ) {
            this.href = "/D_ContactSeller.aspx?property=" + a.join(',');
        } else {
            e.preventDefault();
            alert("Select at least one property to contact!");
            return false;
        }
    });
});
Justin Johnson
Thanks for your reply...I got what you wanna say...thanks a lot.
Sanju
Well that's working Perfectly...Thanks.But i still have a problem. When there is no checkbox is selected it gives javascript message. And after clicking 'ok' it starts loading a false Iframe page over the page. And on single click of mouse it goes out.Do you have any idea how can i Remove this problem?Thanks in advance..
Sanju
What iframe are you talking about? I don't see any in the code that you posted. Also, you can try returning false from the `else` statement as well.
Justin Johnson