0

I have the below element:

<input type="text" value="" tabindex="1" size="15" onblur="UpCaseSearch(this)" name="name">

which is an empty text field.

I want to set text into that field and I can successfully do that using a command like that (when I say successfully I mean that I can see that field populated with text test:

window.frames[1].document.getElementsByName('name')[2].value = "test"

I have 2 questions however:

  1. When I look at that element on the page I see that the value attribute is still empty "". Why is that? Where is the actual text that I can see in that field is coming from?
  2. If I try the below command, it will actually set the value but then the field remains empty:

    window.frames[1].document.getElementsByName('name')[2].setAttribute('value', 'test')

So it looks like that's not the same value in both cases. Is that right?

Eugene S
  • 6,709
  • 8
  • 57
  • 91

2 Answers2

0

In short, the value attribute in source-code (and dom) serves as 'defaultValue'.

For example, imagine this input-field (which is a common way to use this functionality):

<input type="text" 
      value="your name here" 
    onfocus="if (this.value===this.defaultValue) this.value='';" 
     onblur="if (this.value==='') this.value=this.defaultValue;" >

In other words, to set the default value in html-markup (<input value="your_value" type="text">) and from javascript you set the value-attribute (using the methods: var val=elm.getAttribute('value') and elm.setAttribute('value', val).
This is also why (as you have seen) these attributes propagate to the element's outerHTML (and thus the element's parent innerHTML).

However to get/set the element's current value property you use: var val=elm.value and elm.value=val.

Sidenote: Almost the same goes for a textarea: you can use .defaultValue on a textarea to retrieve it's textContent (but you'd override/change that by setting it's innerHTML) while you get/set the current value using elm.value.

Hope this helps!

GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
0

Try this:

window.frames[1].document.getElementsByName('srchsnam')[2].value = "Test"

Let me know if it worked!

Captain Planet
  • 408
  • 3
  • 19
  • 1
    Have you even read the question? – Ram Nov 13 '14 at 05:14
  • It reads that he is success in setting the value attribute but the values is not seen in the textfield. isn't that his problem? – Captain Planet Nov 13 '14 at 05:17
  • @Vohuman This is probably my fault as I accidentally posted the same line with different values for class name. I fixed it later but this answer was given prior to that. – Eugene S Nov 13 '14 at 06:28