Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

javascript - How to send value to an div which has textbox role

I am trying to send a new value to a div which has textbox role and I am getting this error:

Uncaught TypeError:document.querySelector(...).value is not a function at <anonymous>:1:59

HTML CODE

<div class="msg-form__contenteditable t-14t-black--light t-normal flex-grow-1 full-height notranslate"contenteditable="true" role="textbox"aria-multiline="true" aria-label="Write a message…">?
<p>
<br>
</p>
</div>

MY CODE It finds the element, but it doesn't send the value.

document.querySelector('[aria-label="Writea message…"]').value("Paul")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's some light reading on the textbox role. Straight from there:

It is a better practice to use an <input> element with type="text", or a <textarea> element instead of the ARIA textbox role. When using either semantic element, the ARIA textbox role is not necessary. See Notes on Using ARIA in HTML.

If you're set on using a <div>, value isn't a valid attribute for it, and you'll need to use .innerText or innerHTML

document.querySelector('[aria-label="Write a message…"]').innerHTML = 'Paul';

Also: .value() isn't a function (that's the error you're receiving). If you're trying to set the value of an element that value is a valid attribute for, you'll use element.value = 'Paul'; with JavaScript, or element.val('Paul'); with jQuery


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...