yavg
yavg

Reputation: 3051

How can I access a property of an html element?

I'm trying to get the "validacion" property of an HTML element.

console.log (element[0]);

This returns me:

enter image description here

<input class="estilo_input_text ng-pristine ng-untouched ng-valid ng-empty" id="asunto" name="asunto" ng-model="asunto" placeholder="Asunto" type="text" validacion="required">

How can I access the "validacion" property?

Upvotes: 3

Views: 10583

Answers (6)

Sujal Mandal
Sujal Mandal

Reputation: 1029

Use the element[0].getAttribute("attribute_name"); method.

Upvotes: 1

huan feng
huan feng

Reputation: 8623

Don't know why you need to do this:

You'd better use the directive way to get the element.

Or you can use Angular jqLite API:

angular.element('#asunto').attr('validacion')

Upvotes: 1

Sajeetharan
Sajeetharan

Reputation: 222522

You can simply do with getAttribute:

console.log(element[0].getAttribute("validacion"));

Upvotes: 0

I think this is the answer you're looking for.

Since you're using Angular and you're implementing ng-model="asunto" why not make it like this in html.

<input class="estilo_input_text ng-pristine ng-untouched ng-valid ng-empty" id="asunto" name="asunto" ng-model="asunto" placeholder="Asunto" type="text">

<button ng-click="get_model(asunto)"></button>

In your JS:

$scope.asunto = "model";

$scope.get_model = function (string) {
  console.log(string)
}

I think this might work becuase you're already using ng-model why not use ng-model instead.

Upvotes: 0

You need the Element.getAttribute() method:

console.log(element[0].getAttribute("validacion"));

Upvotes: 7

Dongin Min
Dongin Min

Reputation: 429

Try element[0].attributes['validacion'].value

Have a good day!

Upvotes: 2

Related Questions