How to verify if a certain value is present in a custom attribute using javascript or jquery?

Arcanadian Arc

Lets say we have a custom attribute named data-content. Now lets look for the html markup :

 <h1 data-content="one two three four">Hello</h1>

I want a Javascript / jQuery function that can do something like this :

 verify.contains("h1", "two"); // => true
 verify.contains("h1", "five"); // => false

Or

 $("h1").verifyContains("two"); // => true
 $("h1").verifyContains("five"); // => false

So, how to do it ?

User863

Using Attribute Contains Word Selector

var verify = {
  contains: function(selector, content) {
    return Boolean($(selector + '[data-content~=' + content + ']').length)
  }
}

console.log(verify.contains("h1", "two"))
console.log(verify.contains("h1", "five"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<h1 data-content="one two three four">Hello</h1>

Jquery plugin function

$.fn.verifyContains = function(content) {
  return $(this).is('[data-content~=' + content + ']')
}

console.log($("h1").verifyContains("two"))
console.log($("h1").verifyContains("five"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<h1 data-content="one two three four">Hello</h1>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

To get Custom attribute value present in <ul> tag using jquery

Get value of a Custom Attribute using Javascript or Jquery

How to verify an attribute is present in an element using Selenium WebDriver?

How to get the value of custom HTML attribute using JQuery?

How do I get the class attribute value using jquery and javascript

How to check if attribute is present in value using Mule expression language

Jquery How to read custom attribute value

How to change HTML custom attribute value by JavaScript?

How to query a data attribute value in javascript (no jquery)?

How to get the value of data attribute using jquery

How to add dynamically attribute value using Jquery?

how to 'split' a css attribute/value using Jquery

How to get data attribute value using jquery

How to access the value of a data attribute using jQuery

How to get to attribute value using jquery

In Apigee, how to get a custom attribute value for a developer using the AccessEntity policy and later in Javascript?

Get custom attribute value from multiple select option using Jquery

Get value of a custom attribute with jquery

Jquery alert the value of a custom attribute

Change the value of span using Javascript or Jquery using <input> or text attribute

How to get the value of input field by custom attribute in jquery

How to use selected in option attribute in name attribute using Javascript or jQuery?

How to change src attribute of iframe based on <select> value using javascript/jquery?

How do I extract text from an attribute value in javascript/jquery using regex?

How can I verify only certain properties using the Verify() method?

if/else jQuery using custom attribute

custom value in custom attribute jquery select option

Custom JSON deserialization only if certain field is present (using Jackson)

how to change the custom attribute value using angularjs controller?