-2

I have this perfectly working two jQuery functions, and I need to pass parameter to it, as I need to call these functions multiple times with different parameters.

$('#link-31').click(function() {
    $(document.getElementById('row-31')).css("background-color", "#B3B3FF");
    $(document.getElementById('row-31')).css("border", "#0000FF");
});

$(document).on('click', function(e) {
    if (!$(e.target).closest('#link-31').length) {
        $('#row-31').css("background-color", '');
        $('#row-31').css("border", '');
    }
});

In the above code "link-31" and "row-31" are id of two html components. These are the two values that I need to pass as parameters. So how can this be done?

Thanks in advance.

The first function is executed on click of link(i.e. tag) with id="link-31", that highlights a table row(i.e. tag) with id="row-31".

The second function is executed by clicking anywhere on the page other than the highlighted row that removes the highlighting.

Now there are assume 'n' number of links that are to be used for highlighting 'n' number of rows, in this case only the link id and row id will change in the above code.

Below is the part of html file that is affected by the above code.

<body>
<ul class="sidebar-nav" style="padding-top: 70px">  
    <nav class="bs-docs-sidebar hidden-print hidden-xs hidden-sm affix col-sm-3">
        <li id="link-1" class="list-group-item"><a href="#data-1" class="scroll">1</a></li>
        <li id="link-2" class="list-group-item"><a href="#data-2" class="scroll">2</a></li>
        <li id="link-3" class="list-group-item"><a href="#data-3" class="scroll">3</a></li>
        .               
        .
        .
        <li id="link-31" class="list-group-item"><a href="#data-31" class="scroll">31</a></li>
        <li id="link-32" class="list-group-item"><a href="#data-32" class="scroll">32</a></li>
    </nav>          
</ul>


<table class="table table-bordered table-striped">
    <thead>
        <tr>
            <th>#</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Username</th>
        </tr>
    </thead>
        <tbody>
        <tr id="row-1">
            <th scope="row"><a id="data-1" class="scroll">1</a></th>
            <td>Mark</td>
            <td>Otto</td>
            <td>@mdo</td>
        </tr>
        <tr id="row-2">
            <th scope="row"><a id="data-2" class="scroll">2</a></th>
            <td>Jacob</td>
            <td>Thornton</td>
            <td>@fat</td>
        </tr>
        .
        .
        .
        <tr id="row-32">
            <th scope="row"><a id="data-32" class="scroll">32</a></th>  
            <td>Larry</td>
            <td>the Bird</td>
            <td>@twitter</td>
        </tr>
    </tbody>
</table>

Sushant Nadkar
  • 183
  • 3
  • 10
  • 1
    Please elaborate and Also it would be better if you show your HTML. – Satpal Aug 27 '15 at 11:17
  • 1
    possible duplicate of [jquery .click pass parameters to user function](http://stackoverflow.com/questions/3273350/jquery-click-pass-parameters-to-user-function) – DFayet Aug 27 '15 at 11:18
  • if you want only id, you can use **this** inside function. – Arunachalam Aug 27 '15 at 11:18
  • I notice you have link-31 and row-31 which may suggest you have other elements that match by ID. If you post your HTML we may be able to show you how to do this programatically, i.e. not have a statement for each matching pair. – Popnoodles Aug 27 '15 at 11:38

4 Answers4

1

Instead of passing the values to method you can use the below code to get the id

var id = $(this).attr('id');//link-31
Vivek Ranjan
  • 1,432
  • 2
  • 15
  • 37
0

I think you should do it another way.

In your #link-31 keep an attribute with target node ID. like:

<div id="link-31" data-target="row-31"></div>

And then bind it like:

$('#link-31').click(function () {
    $("#" + $(this).data("target")).css({
        "background-color": "#B3B3FF",
        "border": "#0000FF"
    });
    //Note:  I think, toggling a class is better option here rather than changing inline CSS
});

So, here appending data-target attribute in HTML node will work like passing parameter in function. And later instead of using ID in HTML elements, put a class and then bind the event on that class, so that that works globally in your page.

Ashish Kumar
  • 2,991
  • 3
  • 18
  • 27
0

in event handler functions like .click() event.target represents the element where event was fired.

To retrieve html attributes from it just do .attr('attributeName');

$(event.target).attr('id'); //retrieve html id attribute! 
k1r0s
  • 359
  • 3
  • 14
-1

It appears that you want the row highlighted when the control is clicked, and undone when anything but the control is clicked. As for passing parameters, we could do with seeing the HTML, as the IDs for the rows can be sought programatically.

Demo

<div id="link-31" class="link">link-31</div>
<h1 id="row-31" class="row">row-31</h1>

<div id="link-32" class="link">link-32</div>
<h1 id="row-32" class="row">row-32</h1>

jQuery

$(document).on('click', function(e) {
    // clear all
    $('.row').css({
        backgroundColor: "",
        border: ""
    });

    // check the target and change the correct row
    if ($(e.target).hasClass('link')) {
        var id = $(e.target).attr('id').replace('link-', '');
        $('#row-' + id).css({
            backgroundColor: "#B3B3FF",
            border: "#0000FF"
        });
    }
});

I suggest using addClass() and removeClass() instead of adding the CSS in javascript.

Popnoodles
  • 28,090
  • 2
  • 45
  • 53