1

Topic: property vs attribute

I read this chapter:
http://learn.javascript.ru/attributes-a … properties

wrote:

Change of some properties updates attribute. But it is faster an exception, than a rule.
To thicket synchronization - one-sided: property depends on attribute, but not on the contrary.
For example, at property change input.value the attribute input.getAttribute (' value ') does not change:

<body>
<input id = "input" type = "text" value = "markup">
<script>
input.value = ' new ';//changed property
alert (input.getAttribute (' value '));//' markup ', did not change!
</script>
</body>

That is, DOM-property change value does not influence attribute, it remains same.
And here attribute change updates property:

<body>
<input id = "input" type = "text" value = "markup">
<script>
input.setAttribute (' value ', ' new ');//changed attribute
alert (input.value);//' new ', input.value changed!
</script>
</body>

This singularity can be used beautifully.
It turns out that the attribute input.getAttribute (' value ') stores original (initial) value even after the user filled a field and property changed.
For example, it is possible to take initial value from attribute and to compare to property, to learn, whether value changed. And if needed and to re-record property attribute, canceling changes.

I did not understand the last paragraph.
Here I wrote such example:

<body>
<input id = "input" type = "text" value = "markup">
</body>
input.setAttribute (' value ', ' new ');//changed attribute
alert (input.value);//' new ', input.value changed!
alert (input.getAttribute (' value '));

What original value did not exchange I did not understand. Probably the author meant something other. Explain

2

Re: property vs attribute

Well and still write that change  influence property
I do something not so?
[img=http://joxi.ru/1A5bN9KfKq9zKr.jpg]
At me it turns out that property change influences property, and change  on  and only.
Change  properties do not influence and on the contrary.

3

Re: property vs attribute

questioner wrote:

What original value did not exchange I did not understand. Probably the author meant something other. Explain

Work with property, instead of attribute - then the attribute will not change.
Here it to you the author also wanted to inform...

4

Re: property vs attribute

questioner wrote:

I do something not so?

Probably. smile))
Therefore as all is extreme simple...

<! DOCTYPE html>
<html>
<head>
<meta http-equiv ='Content-Type ' content ='text/html; charset=windows-1251 '/>
<! -
<script src ='http://code.jquery.com/jquery-latest.js '> </script>
<script src = "https://code.angularjs.org/1.3.9/angular.min.js"> </script>
<script src = "https://code.angularjs.org/1.3.9/angular-route.js"> </script>
-->
<style type ='text/css'>
</style>
<script type ='text/javascript '>
</script>
</head>
<body>
<input id = "test" type = "text" value = "old">
<script type ='text/javascript '>
var obj=document.getElementById (' test ');
obj.setAttribute (' value ', ' new ');
alert (' 1. ' +obj.getAttribute (' value ', ' new '))
alert (' 2. ' +obj.value)
obj.value ='test ';
alert (' 1. ' +obj.getAttribute (' value ', ' new '))
alert (' 2. ' +obj.value)
</script>
</body>
</html>

I.e. attribute change changes attribute and property... And here property change changes only property, the attribute does not change.

5

Re: property vs attribute

krvsa wrote:

obj.getAttribute (' value ', ' new ')

To read as

obj.getAttribute (' value ')

Hurried at copying... sad((

6

Re: property vs attribute

krvsa wrote:

I.e. attribute change changes attribute and property... And here property change changes only property, the attribute does not change.

I all am finite, I understand, but unless I did not give a refutation to what it does not work for me and  a real example?

7

Re: property vs attribute

questioner;
Here I do not understand, what for to you it?
The reality is that that is

$(elem).attr (' value ');
$(elem).prop (' value ');
$(elem).val ();

You should understand when and what to use.
What for to be engaged in "hacking"? We admit you on 100 % understood for all browsers when and that is used, started it to apply, comes  which does not know these nuances, your code corrects and all ceases to work... You will be guilty.

8

Re: property vs attribute

questioner wrote:

but unless I did not give a refutation to that it does not work for me

There is at you no "refutation".

questioner wrote:

and  a real example?

No example is present.
Here I have an example (see above), you can copy it in a file  and launch, look as it works for you...
And here at you such examples still are not present.