3

I want to select all the inputs with name attribute end with *Name.

I have 6 inputs type text with name :

  • deviceName
  • profileName
  • ssidName
  • captiveName
  • trafficName
  • sessionName

HTML

<input class="form-control" type="text" name="deviceName">
<input class="form-control" type="text" name="profileName">
<input class="form-control" type="text" name="ssidName">
<input class="form-control" type="text" name="captiveName">
<input class="form-control" type="text" name="trafficName">
<input class="form-control" type="text" name="sessionName">

I'm trying to prevent any space enter on those 5 inputs

I've tried

$("input[name='*Name']").keyup(function() {
    this.value = this.value.replace(/\s/g, '');
});

My selection doesn't seem to take any effects.

5

3 Answers 3

3

Just a small error in your code with the attribute contains selector -

Change:

$("input[name='*Name']")

to

$("input[name*='Name']")

Check this JSBin for playing around.

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

Comments

2

I think you are using the correct selector but in the wrong place.

you need to use it like so $("input[name*='Name']")

See the link to jquery documentation. https://api.jquery.com/attribute-contains-selector/

Comments

1

To select element ending with attribute value 'Name' use $("input[name$='Name']") - https://api.jquery.com/attribute-ends-with-selector/.

$("input[name$='Name']").keyup(function() {
  this.value = this.value.replace(/\s/g, '');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="form-control" type="text" name="deviceName">
<input class="form-control" type="text" name="profileName">
<input class="form-control" type="text" name="ssidName">
<input class="form-control" type="text" name="captiveName">
<input class="form-control" type="text" name="trafficName">
<input class="form-control" type="text" name="sessionName">

Note - If you want to make attribute selector case-insensitive add i before closing square bracket i:e $("input[name$='name' i]").

console.log($("input[name$='name' i]"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="form-control" type="text" name="deviceName">
<input class="form-control" type="text" name="profileName">
<input class="form-control" type="text" name="ssidName">
<input class="form-control" type="text" name="captiveName">
<input class="form-control" type="text" name="trafficName">
<input class="form-control" type="text" name="sessionName">

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.