0

I have a button as follows:

<a href="#" onclick="AddRow();return false;" class="btnA">ADD</a>

Once I click the button, it adds a row to a #dataList1. Then I want it to be hidden. And I did following in Javascript;

<script type="text/javascript">

function AddRow(){
var tr = '<tr>';
tr += '<td></td>';
tr += '<td><span class="br"><input type="text" id="grpNm" name="grpNm" value="" style="width:96%"/></span></td>';
tr += '<td><span class="br"><input type="text" id="grpExp" name="grpExp" value="" style="width:96%"/></span></td>';
tr += '</tr>';
    $('#dataList1').append(tr);
};

$('#AddRow').on('click', function(){
$('#AddRow').hide();
}); 
</script>

It adds the row, but after it, it does not hide the button. Where did I do wrong?

6
  • add $('.btnA').css('display', 'none'); Commented Jan 13, 2014 at 9:39
  • 1
    I've removed the <JAVASCRIPT> from the beginning of your title. Don't put tags in the title. (Do you see anyone else putting tags in the title?) Commented Jan 13, 2014 at 9:39
  • I'm curious why $('#AddRow').on('click', function(){ $('#AddRow').hide(); }); is not working?? Commented Jan 13, 2014 at 9:41
  • Also, if you're using jQuery (and you clearly are), include the jquery tag on the question. Commented Jan 13, 2014 at 9:42
  • try $('#AddRow').style.disply="none" Commented Jan 13, 2014 at 9:42

4 Answers 4

2

Add id to input

HTML

    <a href="#" onclick="AddRow();return false;" class="btnA" id="AddRow">ADD</a>
    <div id="dataList1"></div>

And hide button after appending in AddRow function.

JS

function AddRow(){
var tr = '<tr>';
tr += '<td></td>';
tr += '<td><span class="br"><input type="text" id="grpNm" name="grpNm" value="" style="width:96%"/></span></td>';
tr += '<td><span class="br"><input type="text" id="grpExp" name="grpExp" value="" style="width:96%"/></span></td>';
tr += '</tr>';
    $('#dataList1').append(tr);

    $("#AddRow").hide();
}

fiddle

Sign up to request clarification or add additional context in comments.

3 Comments

You are also adding the $('#AddRow').hide() inside AddRow() function. In that case, you don't need to add 'id' explicitly when you can refer button using its class.
Yes, "And hide button after appending in AddRow function.". I've edited my fiddle to be more clear.
My question is why you need to give id when that button can be referred by its class?
1

Because there is no control with the id AddRow. You have to either give id or use class sector $('.btnA').hide();

<script>
function AddRow(){
var tr = '<tr>';
tr += '<td></td>';
tr += '<td><span class="br"><input type="text" id="grpNm" name="grpNm" value="" style="width:96%"/></span></td>';
tr += '<td><span class="br"><input type="text" id="grpExp" name="grpExp" value="" style="width:96%"/></span></td>';
tr += '</tr>';
$('#dataList1').append(tr);
$('.btnA').hide();
</script>

Comments

0

You can hide the button in AddRow() function itself:

function AddRow(){
  var tr = '<tr>';
  tr += '<td></td>';
  tr += '<td><span class="br"><input type="text" id="grpNm" name="grpNm" value=""  style="width:96%"/></span></td>';
  tr += '<td><span class="br"><input type="text" id="grpExp" name="grpExp" value=""   style="width:96%"/></span></td>';
  tr += '</tr>';
  $('#dataList1').append(tr);
  $('.btnA').hide();
};

DEMO

Comments

0

Three Steps Problems:

1) Add this code inside AddRow() function:

$('#AddRow').hide();

2) Remove This event:

$('#AddRow').on('click', function(){

3) Add id AddRow in

<a href="#" onclick="AddRow();return false;" class="btnA">ADD</a>

Comments

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.