Get value of an attribute in jquery when marking it

1

I have an input such that

<input type="checkbox" id="numero-id="' + numeroID + '" />

How do I get the value of numeroID ?

I have many input

<input type="checkbox" id="numero-id=6" />
<input type="checkbox" id="numero-id=2" />
<input type="checkbox" id="numero-id=3" />
<input type="checkbox" id="numero-id=7" />
<input type="checkbox" id="numero-id=1" />
<input type="checkbox" id="numero-id=8" />
<input type="checkbox" id="numero-id=9" />

I want to take it out when I mark it and only the one that I marked

That is, if I mark the one with numero-id=6

<input type="checkbox" id="numero-id=6" />

I want you to give me back

6

How would the activator or the identifier be when dialing? And the function so that it recovers the value of numeroID of that marked input?

    
asked by Paolo Frigenti 27.04.2018 в 13:06
source

2 answers

3

I think this is what you want:

$('input[type="checkbox"]').change(function() {
	var id = this.id.replace("numero-id=","")
	$("#seleccionado").html(id)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="checkbox" id="numero-id=6" />
<input type="checkbox" id="numero-id=2" />
<input type="checkbox" id="numero-id=3" />
<input type="checkbox" id="numero-id=7" />
<input type="checkbox" id="numero-id=1" />
<input type="checkbox" id="numero-id=8" />
<input type="checkbox" id="numero-id=9" />

<h1 id="seleccionado"> </h1>

I hope it serves you

    
answered by 27.04.2018 / 13:25
source
1

There will be many ways to get to the same point, it occurs to me that, when you click on one of the checkboxes, pick up your id and separate it by "=". That is:

$("input").click(function(event) {
    var id = event.target.id; 
    var num = id.split("="); 
    console.log(num[1]); 
});

In this way, with the first line of the function, you collect all the id of the checkbox clicked numero-id=6 for example.

In the next one you create an array with two values, the 0 will be the same as the previous one and the 1 the later one, that is to say num = ["numero-id"]["6"]

With the last one, you show the value of the last element of the array, which corresponds to the number you are looking for.

You could simplify everything quite as follows:

$("input").click(function(event) {
        console.log(event.target.id.split("=")[1]);
});

With event.target.id.split("=")[1]) the value you're looking for.

    
answered by 27.04.2018 в 13:27