How to set an attribute with a blank value?

SteveC

When I use the following:

myElement.setAttribute('data-title', title);

where title is blank I don't get a blank attribute. I get an attribute with no value.

Is there a way to get the blank attribute using either JavaScript or jQuery? I ask because if I can't then the code I have that references the attribute later is going to become complex to deal with the possibility of there being no value.

A-312

setAttribute is native function.

var myElement = document.getElementById("myId");

myElement.setAttribute('data-title', "")

myElement.getAttribute('data-title'); // you get : ""

myElement.getAttribute('data-test'); // you get : null


if (myElement.getAttribute('data-title') !== "" && myElement.getAttribute('data-title') !== null)
    console.log("not empty");

If you want to use jQuery prefere, attr :

var $myElement = $("#myId");

$myElement.attr("data-title", "");

$myElement.attr("data-title"); // you get : ""

$myElement.attr("data-test"); // you get : undefined

if (myElement.getAttribute('data-title') !== "" && typeof(myElement.getAttribute('data-title')) !== "undefined")
    console.log("not empty");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to set an attribute with a blank value?

From Dev

How to set a value to an attribute with Javascript

From Dev

How to set CKEditor default value to blank?

From Dev

How to not remove a blank attribute

From Dev

Ruby - How to set value for dynamic attribute in class

From Dev

How set list value for attribute of class in python

From Dev

How to set eWay value attribute using javascript

From Dev

How to set the default value of directive attribute?

From Dev

How to set as attribute an attribute value from another class in Python

From Dev

How can I set the value of textbox to "0" if blank or empty?

From Dev

How can I set the value of textbox to 0 if blank or empty?

From Dev

How to set default value as blank in dropdown along with items tag

From Dev

XSL transformation - how to set attribute value to element value

From Dev

Replace blank Attribute with value in XML with XSLT transformation

From Dev

How is this returning a blank value?

From Dev

How to set a default attribute value for a Laravel / Eloquent model?

From Dev

How to set a default value for attribute if it doesn't exist in the XDocument object

From Dev

Java: how to set default value to annotation with another annotation as its attribute?

From Dev

how to set "data-value" Attribute of div in angular 2

From Dev

How to access the data attribute value using data set

From Dev

How to set the content of an element to value of its attribute with jQuery

From Dev

how to dynamically set the "value" attribute of url tag in struts2?

From Dev

How to set value into textarea attribute using nightwatch.js

From Dev

OOP - How to demand derived classes to set a value to base attribute?

From Dev

how to set value of attribute in a class and convert it into json using lift json

From Dev

VertexArrayObject, how can i set "default" attribute value?

From Dev

How to set and return virtual attribute value got from request in Yii

From Dev

How do i set value attribute to a div tag?

From Dev

How to set null value to an existing attribute on Entity Framework

Related Related

  1. 1

    How to set an attribute with a blank value?

  2. 2

    How to set a value to an attribute with Javascript

  3. 3

    How to set CKEditor default value to blank?

  4. 4

    How to not remove a blank attribute

  5. 5

    Ruby - How to set value for dynamic attribute in class

  6. 6

    How set list value for attribute of class in python

  7. 7

    How to set eWay value attribute using javascript

  8. 8

    How to set the default value of directive attribute?

  9. 9

    How to set as attribute an attribute value from another class in Python

  10. 10

    How can I set the value of textbox to "0" if blank or empty?

  11. 11

    How can I set the value of textbox to 0 if blank or empty?

  12. 12

    How to set default value as blank in dropdown along with items tag

  13. 13

    XSL transformation - how to set attribute value to element value

  14. 14

    Replace blank Attribute with value in XML with XSLT transformation

  15. 15

    How is this returning a blank value?

  16. 16

    How to set a default attribute value for a Laravel / Eloquent model?

  17. 17

    How to set a default value for attribute if it doesn't exist in the XDocument object

  18. 18

    Java: how to set default value to annotation with another annotation as its attribute?

  19. 19

    how to set "data-value" Attribute of div in angular 2

  20. 20

    How to access the data attribute value using data set

  21. 21

    How to set the content of an element to value of its attribute with jQuery

  22. 22

    how to dynamically set the "value" attribute of url tag in struts2?

  23. 23

    How to set value into textarea attribute using nightwatch.js

  24. 24

    OOP - How to demand derived classes to set a value to base attribute?

  25. 25

    how to set value of attribute in a class and convert it into json using lift json

  26. 26

    VertexArrayObject, how can i set "default" attribute value?

  27. 27

    How to set and return virtual attribute value got from request in Yii

  28. 28

    How do i set value attribute to a div tag?

  29. 29

    How to set null value to an existing attribute on Entity Framework

HotTag

Archive