最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Set the value of element by classname, without having the element's ID - Stack Overflow

programmeradmin0浏览0评论

Assume that we have the following code:

<span class="input-text">
    <input type="search">
</span>

Is there any way, we could set the value of that without having the ID of the element?

Assume that we have the following code:

<span class="input-text">
    <input type="search">
</span>

Is there any way, we could set the value of that without having the ID of the element?

Share Improve this question edited Jan 22, 2014 at 11:44 Vucko 20.8k12 gold badges59 silver badges108 bronze badges asked Jan 22, 2014 at 11:41 ManoMano 5271 gold badge8 silver badges21 bronze badges 1
  • The question is little unclear to me . your saying set value of element by class name and you did not give a class to your input tag instead to span tag please be little more specific , what does that mean in your question ? input or span – niko Commented Jan 22, 2014 at 12:38
Add a ment  | 

7 Answers 7

Reset to default 3

Possible solutions are many. .val('your value') is the method to set the input tag value

jQuery

With class referring to span element
$('.input-text').find('input').val('value');
$('.input-text input').val('value'); // descendent selector
$('.input-text > input').val('value'); // direct children selector
Without referring to span element
$('input[type=search]').val('value'); // attribute selector

Javascript

$('.input-text').find('input')[0].value = 'value' 

Or If you want to over write the content inside the <span> tag use

$('.input-text').html('content')
$('span.input-text').html('content')

You can do like this :-

$(".input-text").children().val('set any value you want to')

Use like this:

$('input[type="search"]').val("set your value");

You can use Attribute Equals Selector [name="value"]

$('input[type="search"]').val("Your Value");

or

$('.input-text input').val("you value");

DEMO

You should refer jQuery selectors. This would be more helpful to you.

try val() or value

$('input[type="search"]').val("Your Value");

or

$('input[type="search"]').value = "Your Value"

Try this:

$(".className").val("your value");
发布评论

评论列表(0)

  1. 暂无评论