1

I have an issue with automatic name for input, I'll try to explain what i need to do. i have an id, that I get it from an external function. I need to use this numeric id to create another function like that.

var id = 10;  // this is the id (from external function)
var value = "n"+bar.toString(); // (I try to cast the id as string) 
$("input[name="+value+"]").on('change', function() { // use the cast value to name my input.
  alert($("input[name="+value+"]:checked", "#myForm").val()); 
});

When I try to do that I get undefined, but when I change the id like that var id ="10" I get the correct answer, but I have a numeric input. Please help me figure out how to solve this problem.

10
  • 2
    i did't see you cast the id to string. bar.toString(); should be id.toString(); and what is n for?? Commented Mar 24, 2015 at 11:06
  • Here, 'n' refers to...? Commented Mar 24, 2015 at 11:07
  • my inputs are like that : n10 , n20 .... its just a name Commented Mar 24, 2015 at 11:07
  • then use id.toString(); Commented Mar 24, 2015 at 11:08
  • 2
    How about var value = "n" + 10; There's no need to do toString() Commented Mar 24, 2015 at 11:11

3 Answers 3

2

Did you want something like this? This is based on an assumption that you have checkboxes within a form!

var ids = [10, 20, 30, 11, 12];
$.each(ids, function(index, val) {
    var id = val;
    var value = "n" + id; // `.toString` is not required!
    $("#myForm").find("input[name='"+value+"']").on('change', function() {
        if ($(this).is(":checked")) {
            alert( $(this).val() );
        }
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="myForm">
    <input type="checkbox" name="n10" value="10" />
    <input type="checkbox" name="n11" value="11" />
    <input type="checkbox" name="n12" value="12" />
</form>

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

2 Comments

can u use that inside a loop and show it to me , for exemple array {10,20,30} and iterate all the code and each time put the value in id and see if you can manage handle all the checkboxes ?
Edited my answer. Take a look.
2

use this code no need for id.toString()

var id = getId();  // this is the id (from externel function)
var value = "n" + id;
$("input[name="+value+"]").on('change', function() {
    alert($("input[name="+value+"]:checked").val());  //change this selector accordingly
});

function getId() {
    return 10;
}

here is the fiddle https://jsfiddle.net/rrehan/srhjwrz4/

Comments

0

Try below code:

var id = 10;  // this is the id (from externel function)
              var value = id.toString(); // (i try to cast the id as string) 
            console.log(value);
            $("input[name="+value+"]").on('change', function() { // use the casted value to name my input.
                  alert($("input[name="+value+"]:checked", "#myForm").val()); 
                });

Demo Link

1 Comment

i am using a loop i guess this why i have this problem ? no ? the externel ids are genereted from an array could this be the problem

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.