Site icon Puneet Sharma – Freelance Web Developer

How to use use jQuery to get the name value after the prefix string for input elements?

var inputName = $('input[name="bodySample-1"]').attr('name');
var prefix = 'bodySample-';
if (inputName.indexOf(prefix) === 0) {
    var value = inputName.substr(prefix.length);
    console.log(value);
}

This code will log the extracted value for each input element that starts with the “bodySample-” prefix. If you only want to extract the value for a specific input element, you can modify the selector to select that element specifically.

Here we check if the name attribute starts with the prefix using the indexOf() method. If it does, we use the substr() method to extract the value after the prefix. Finally, we log the extracted value to the console.

Exit mobile version