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!

3
  • 2
    value="checked()" really ?? Commented Mar 1, 2014 at 13:25
  • I first had onclick, the only thing was that onclick is after and what I want is the value. Commented Mar 1, 2014 at 13:33
  • I should note that you can't name a function checked() as it is a boolean value. Consider naming it isChecked() Commented Mar 1, 2014 at 13:38

5 Answers 5

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".....

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

Comments

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"); 
});

3 Comments

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 Are you sure your button has the input type of 'submit'? Are you using a button or an input?
A button, <input type="submit" name="submit" class="btn btn-success" value="Verwijder" />
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);
});

5 Comments

And what needs to be the value of the input button? Thank you for helping.
Whatever you want. It will be changed afterwards using .val(). Use a string rather than a function return.
You shouldn't use attr() as it will get the value it loaded as. Instead, use prop(). See prop() vs attr(): stackoverflow.com/questions/5874652/prop-vs-attr
@Spedwards oh ok. Never seen that before.
He won't change it, check the orginal post for the update. Thanks for helping so far.
0
$('input[type="checkbox"]').on("click","input[type=checkbox]",function()
{
   $("input[type=submit]").val("Delete "+$('input:checkbox:checked').length+" rows"); 
});

4 Comments

And what needs to be the value of the input button? Thank you for helping.
Spedwards - no problem ... I changed it ... but why you post the same solution ??? I'm getting angry
@pooler I just posted my solution. It was a revision of yours.
I think .. $(document).on() .. is not so big problem as you meantioned .. you just give me vote down and "steal my solution"
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

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.