0

Sorry but I'm quite a noob if it comes to Javascript with HTML. I'm trying to make a button that will change the value of it when a multiple checkboxes are checked.

Example: If one checkbox is checked = Button: Delete 1 row If two checkboxes are checked = Button: Delete 2 rows etc.

And I want this to happen automaticly when I check all checkboxes. The only problem is that I don't know how to call the value of it in a button.

JS:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
    $('input[type="checkbox"]').click( function() {
        $("input[type=submit]").val("Delete "+$('input:checkbox:checked').length+" rows"); 
    });
</script>

HTML:

<input type="submit" name="submit" class="btn btn-success"  value="Verwijder" />

The server-side of this(PHP) does work(deleting the rows). Thank you for helping and your time!

RezaM
  • 167
  • 1
  • 1
  • 11

5 Answers5

2

you can change the value of a button by using this function

function change(val)
{
document.getElementById(12345).value=val;
}

while creating button give an id to it like id="12345".....

Hari hara prasad
  • 313
  • 1
  • 14
1

You can simply do this listening to the click event of the checkboxes and applying the value by getting the length of $('input:checkbox:checked')

jsfiddle: http://jsfiddle.net/spedwards/Ls8sp/

$('input[type="checkbox"]').click( function() {
   $("input[type=submit]").val("Delete "+$('input:checkbox:checked').length+" rows"); 
});
Spedwards
  • 4,167
  • 16
  • 49
  • 106
  • I've tried to do this but nothing will change. Thank you for giving a JS Fiddle but nothing won't work. I've changed the orginal post to see what I now have. Thank you for helping so far. – RezaM Mar 01 '14 at 13:54
  • @RezaM Are you sure your button has the input type of 'submit'? Are you using a button or an input? – Spedwards Mar 01 '14 at 14:01
  • A button, – RezaM Mar 01 '14 at 14:03
0
$('input[type="checkbox"]').on("click","input[type=checkbox]",function()
{
   $("input[type=submit]").val("Delete "+$('input:checkbox:checked').length+" rows"); 
});
pooler
  • 327
  • 3
  • 12
0

With jQuery you can change the value of the element within your script.

$('input[type="checkbox"]').click(function(){
    if($(this).attr('checked')===true){
        i++;
    }
    else{
        i--;
    }
    $('input[type="submit"]').val("Delete "+i);
});
bashleigh
  • 8,813
  • 5
  • 29
  • 49
0

What about something like this

HTML

<form>
    <ul>    
        <li><input type="checkbox" />item one</li>
        <li><input type="checkbox" />item two</li>
        <li><input type="checkbox" />item three</li>
        <li><input type="checkbox" />item four</li>
        <li><input type="checkbox" />item five</li>
        <li><input type="checkbox" />item six</li>
        <li><input type="checkbox" />item seven</li>
        <li><input type="checkbox" />item eight</li>
        <li><input type="checkbox" />item nine</li>
        <li><input type="checkbox" />item ten</li>
    </ul>
    <input type="submit" value="Delete (0)" />
</form>

jQuery

var getChecked = function($form) {
    return $form.find('input[type="checkbox"]:checked');
};

$form = $('form').on('submit', function(e) {
    e.preventDefault();
    getChecked($form).each(function() {
        $(this).parent().remove();
    });
});

$form.find('input[type="checkbox"]').on('change', function(e) {
    $form.find('input[type="submit"]').val('Delete (' + getChecked($form).length + ')');
});

see live fiddle

mathieu
  • 41
  • 3