3

I have many elements like

<input  tag="data-initial-222" value="" type="checkbox">

<input   tag="data-initial-111" value="" type="checkbox">

How can I select all the elements who have this tag "data-initial-*"

Another Question is :

How to select

 <input data-initial="111" type="checkbox"> 

or

 <input data-initial="222" type="checkbox"> 

using data-initial attribute.

2
  • I'm not aware of tag attribute. Doesn't seem to be a valid one mdn docs. Why not use data- instead? Commented Jul 5, 2017 at 7:14
  • yes, data-* tag is more common. I had to use tag="data-initial-222" for some requirement Commented Jul 5, 2017 at 7:22

2 Answers 2

2

console.log($("input[data-initial]").length)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input  tag="data-initial-222" value="" type="checkbox">

<input   data-initial="111" value="" type="checkbox">

<input  data-initial="222" value="" type="checkbox">

<input   data-initial="333" value="" type="checkbox">

  1. Use starts with selector

Description: Selects elements that have the specified attribute with a value beginning exactly with a given string.

  1. Attribute selector

    Description: Selects elements that have the specified attribute, with any value.

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

11 Comments

how to select if I have <input data-initial="326" type="checkbox"> from "data-initial" thing
you mean select starting with 326? $("[data-initial^=326]") is this what you mean?
I mean, all the input types with data-initial tag. Plz
change your selector to $("input[tag^=data-initial-").length) this will select all input with tag tag starting data-initial-
no, this is not working, I have now different tagging type. Its data-initial ="111" or data-initial="222"
|
2
$( "input[tag*='data-initial']" )

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.