-3

can we get that attributes that doesen't support with the browser or not? for example pattern attribute or something like this? and is there any differnt beetween javascript and jquery for getting unsupport attribute?

var x = document.getElementById("myAnchor").getAttribute("pattern");
var x = document.getElementById("myAnchor").getAttribute("required");
var x = document.getElementById("myAnchor").getAttribute("somethin else");

i just want the value of pattern attributes , it doesn't matter pattern support or not. i just want the value of an attribute

Peyman abdollahy
  • 799
  • 1
  • 8
  • 18
  • 4
    There's no way `somethin else` could conceivably *be* an attribute, regardless of browser support. – BoltClock Jun 05 '15 at 03:37
  • I think one way to check whether a feature is supported or not is to check is something like `var patternSupported = typeof document.getElementById("myAnchor").pattern == 'undefined'` – Arun P Johny Jun 05 '15 at 03:39
  • 2
    possible duplicate of [Get all Attributes from a HTML element with Javascript/jQuery](http://stackoverflow.com/questions/2048720/get-all-attributes-from-a-html-element-with-javascript-jquery) – abc123 Jun 05 '15 at 03:42

1 Answers1

1

You can get all 'well formatted' attributes. Not just valid ones. https://jsfiddle.net/6tf4zov1/

this will work:

<a my_own_attr='123' id='x'>123</a>
<script type='text/javascript'>
var x = document.getElementById("x").getAttribute("my_own_attr");
alert(x);
</script>

this will NOT:

<a my own attr='123' id='x'>123</a>
<script type='text/javascript'>
var x = document.getElementById("x").getAttribute("my own attr");
alert(x);
</script>

You might want to check with different browser since JS is client side scripting and each browser could handle it differently.

Louis Loudog Trottier
  • 1,367
  • 13
  • 26