0

I want to add data to a selective cell of a table. I wrote this code in JavaScript.

var rowIndex = 10;
var colIndex = 5;
var dateTable = document.getElementById('timeTable');
dateTable.rows[rowIndex].cells[colIndex].innerHTML = "Su";

I wanted to use the same concept in jQuery as:

$('input[name=add_button]').click(function(e) {
    var dateTable = document.getElementById('timeTable');
    var rowIndex = $('[name=days]').val();
    var colIndex = $('[name=times]').val();
    var text = $('input[name = text]').val();
    dateTable.rows[rowIndex].cells[colIndex].innerHTML = text;
    e.preventDefault();
});

My problem is: preventDefault() method should prevent my form to be submitted. It is not preventing form submission. If I remove the dateTable.rows[rowIndex].cells[colIndex].innerHTML = text; line, the form is not submitted. I think there is some problem with this particular line. Please help me sorting this. If there is better option available in jQuery please tell me. I shall be able to use the colIndex and rowIndex to find the position of the cell.

Thanks

6
  • can you post the html ? colIndex and rowIndex might not being set to what you expect and are in fact undefined. Did you see any error in console ? Commented Apr 8, 2013 at 5:01
  • are you getting any javascript error in firebug console... Commented Apr 8, 2013 at 5:05
  • @phobos: No I don't have any error in console Commented Apr 8, 2013 at 5:05
  • @MaheshSapkal: No. No errors.. Commented Apr 8, 2013 at 5:07
  • convert val() to number. Hard to say you aren't getting errors...form might be submitting faster than errors show in console. Put `preventDefault at begining so if errors occur afterwards form won't submit Commented Apr 8, 2013 at 5:08

1 Answer 1

1

Try this:

Add return false; in your form tag like

<form onsubmit="return false;" ......>

</form>

Also check you indexes that these are starting from 0 or 1, if these are starting from 1 then you have to decrease it by 1 before using it,

like dateTable.rows[rowIndex-1].cells[colIndex-1].innerHTML = text;

Actually when you click on button it will prevent its default behavior, not other elements behavior(form in your case).

So, I think it is not possible to prevent your form here.

Alternative Code you can use:

$('form#myForm').on('submit',function(e){
    e.preventDefault();
    var dateTable = document.getElementById('timeTable');
    var rowIndex = $('[name=days]').val();
    var colIndex = $('[name=times]').val();
    var text = $('input[name = text]').val();
    dateTable.rows[rowIndex].cells[colIndex].innerHTML = text;
    return false;
});
Sign up to request clarification or add additional context in comments.

4 Comments

Do I have any jQuery alternative for this statement?
Check my answer once again, I have made some changes in it. Also refer stackoverflow.com/questions/5081674/…
This will not fulfill, what you needed?
Can you please continue this in chat.stackoverflow.com/rooms/27752/…

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.