0

I am trying to collect multiple pieces of data from a checkbox, but I am unsure of how to do this. Right now I have:

<input tabindex="1" type="checkbox" name="friend[]" id="{{$friend}}" value="{{$friend}}" style="display:inline-block;">

Which allows me to collect an id (contained in {{$friend}}) that I need. But I also need the name associated with this id. Is there a way to collect multiple values from a single checkbox? I would need this because I am collecting the data and moving to another form without changing the view. This would be used for javascript which would print out stuff in the view as it is checked (i.e. the id and name).

Here is the javascript:

<script>
$(document).ready(function(){
    var callbacks_list = $('.callbacks ul');
    $('.facebook-friends-larger input').on('ifChecked', function(event){
        callbacks_list.prepend('<li><img src="https://graph.facebook.com/'+this.id+'/picture" alt="" height="50" width="50"><span id="#'+this.id+'">#' + this.id + '</span> is ' + event.type.replace('if', '').toLowerCase() + '</li>');
    });

    $('.facebook-friends-larger input').on('ifUnchecked', function(event) {
        callbacks_list.find('span#'+ this.id).closest('li').remove();
        console.log(this.id);
    });    
});
</script>

Any ideas? Thank you for your help.

4
  • 1
    Would the name be stored somewhere on the server (such that you will have to make a query for it), or you can simply insert it into your HTML? If the latter is the case, you can simply inject the name associated with a specific ID using the HTML5 data attribute, such as data-name and then simply fetch it using $('input').data('name') Commented Oct 11, 2013 at 5:19
  • The name is stored on the server. Commented Oct 11, 2013 at 6:57
  • The id is associated with a specific name in a table. Commented Oct 11, 2013 at 6:57
  • This is actually what I was looking for. Thank you! Leave an answer and I will check mark it! Commented Oct 11, 2013 at 8:06

4 Answers 4

1

Try this this will be helpyou..

 $("input[type=checkbox]").change(function(){
        alert($(this).val()); //get a val
        alert($(this).attr('name')); //get a value of name attribute

    });

Fiddle here

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

Comments

0

If you have the access to the username before the page is loaded (and is therefore able to inject it into the DOM without making ajax queries after pageload or user action), you can store them in HTML5 data- attributes, for example, data-name in the following format:

<input tabindex="1" type="checkbox" name="friend[]" id="{{$friend}}" value="{{$friend}}" data-name="{{name}}" style="display:inline-block;">

You can access the name by simply calling the .data() method in jQuery, i.e. $('input').data('name').

Comments

0

Use:

var name = $("#checkbox-id").attr("name"); // Use whatever method you have to target the checkbox

and so on to get the other values

Comments

0

Try this

HTML

<input tabindex="1" type="checkbox" name="friend[]" id="123" value="{{$friend}}" style="display:inline-block;">test

JS

$(document).ready(function(){
    $("#123").change(function(){
        $(this).val(); //get a val
        console.log($(this).attr('name')); //get a value of name attribute

    });

});

FIDDLE

4 Comments

why you need to change the id of checkbox?
@Subhash: its just a demo, i am just giving a hint that you should workout like this.
are you sure we capture id from this {{$friend}}
but here id pass in diffrent format it is difficult to access it

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.