admin管理员组

文章数量:1400384

I want to set an input aria-control value. But I can't do it using the traditional jQuery way.

My code is this one:

function showMessage() {
  var message = jQuery("#textToDisplay").val();
  $("#example").text(message);
}
<script src=".1.1/jquery.min.js"></script>
<input type="text" id="textToDisplay" />
<input type="button" value="Add message" onClick="showMessage()" />
<input aria-controls="example" type="text">

I want to set an input aria-control value. But I can't do it using the traditional jQuery way.

My code is this one:

function showMessage() {
  var message = jQuery("#textToDisplay").val();
  $("#example").text(message);
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="textToDisplay" />
<input type="button" value="Add message" onClick="showMessage()" />
<input aria-controls="example" type="text">

How can I set the value?

Share Improve this question edited Aug 15, 2017 at 8:32 Terry 66.3k16 gold badges105 silver badges126 bronze badges asked Aug 15, 2017 at 8:27 Fran RodFran Rod 5864 gold badges14 silver badges26 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

Change the selector like this.your selector target the id not the aria-controls so use with attr selector.Also Input not have text() property so change with val()

function showMessage() {
 var message = jQuery("#textToDisplay").val();
 $("input[aria-controls=example]").val(message);
  }
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="textToDisplay" />
<input type="button" value="Add message" onClick="showMessage()" />
<input aria-controls="example" type="text">

For below Terry ment use addeventlistener instead of inline click function.That do the click function in dom like this

$('#click').click(function() {
  var message = jQuery("#textToDisplay").val();
  $("input[aria-controls=example]").val(message);
})
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="textToDisplay" />
<input type="button" id="click" value="Add message">
<input aria-controls="example" type="text">

Use attr selector

Selects elements that have the specified attribute with a value exactly equal to a certain value.

$( "input[aria-controls='example']").val(message);

You can use

$("#myId").attr("aria-controls", myValue);

Hope this helps!

本文标签: javascriptSet value to an ariacontrols inputStack Overflow