1

I have a table with 1 button in each row, on click of button tax shown in that row will be applied to the client and i need to show some indication to end user. I want to change the button css(color, txt ect.) on click for that purpose. Below is the code -

<table class="data report_table">       
                        <tbody>
                        <c:forEach var="taxVO" items="${taxVo}" varStatus="item">
                        <tr class="${item.index % 2 == 0 ? 'odd gradeX' : 'even gradeC'}">                          
                            <td><c:out value="${taxVO.taxName}" /></td>
                            <td><input type="button" class="btn-icon" value="Apply" id="applyTaxButton" onClick="applyTax('${taxVO.taxId}');"></input>
                            </td>   
                        </tr>
                        </c:forEach>
                        </tbody>
                    </table>

and jquery i am using is -

function applyTax(taxIdValue)   {
    $('#taxId').val(taxIdValue);
    $(this).$('#applyTaxButton').css("background-color","yellow");
}

but i am getting error - Uncaught TypeError: Object [object Object] has no method '$'. Can someone please let me know how to fix it?

2
  • Have you tried $(this).find('#applyTaxButton').css("background-color","yellow");? Commented Oct 14, 2012 at 17:36
  • yes i tried, although there was no java script error but nothing happens on the button as well. No change. Commented Oct 14, 2012 at 17:38

2 Answers 2

2

jsFiddle Demo

HTML IDs should be unique, so you should not have the all buttons having the same ID. Use the class attribute for that purpose. However, to simply change the context of your selection, the correct syntax is:

$(selector, context);

A better approach is to remove the onclick attribute altogether, and handle the event completely in jQuery:

$(function() {
    $('.report_table').on('click', '.btn-icon', function() {
        $(this).css('background-color', 'yellow');
        $('.btn-icon').not(this).css('background-color', 'transparent');
    });
});​
Sign up to request clarification or add additional context in comments.

3 Comments

Not working, no error but no change either, i think my JSTL loop is generating more tha 1 button and actual HTML is - <input onclick="applyTax('2');" id="applyTaxButton" value="Apply" class="btn-icon btn-small btn-grey" type="button">, this might cause the jquery hard to find exect button
even though i made it dynamic(id) by adding loop counter value with button id, still it doesn;t work :(
Removing onClick() also doesn't help. :(
1
<input id="taxId" type="text" /><br />

<input onclick="applyTax(this, '1');" value="Apply" class="btn-icon btn-small btn-grey" type="button" />
<input onclick="applyTax(this, '2');" value="Apply" class="btn-icon btn-small btn-grey" type="button" />
<input onclick="applyTax(this, '3');" value="Apply" class="btn-icon btn-small btn-grey" type="button" />


<script>
function applyTax(btn, taxIdValue)   {
    $('#taxId').val(taxIdValue);
    $(btn).css("background-color","yellow");
}
</script>

Demo: http://jsfiddle.net/NFGkj/

But it's better to use jquery event binding.

2 Comments

You solution has 1 issue, i have to undone yellow once another apply button is clicked. only 1 can be of yellow color out of array of apply buttons
@Sachin I just show idea, for one yellow button try this: jsfiddle.net/NFGkj/1 or jsfiddle.net/NFGkj/2

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.