I have following code for the textbox:
<label>Enter the doctor id</label>
<input type="text" autocomplete="on" name="dr_id" id="user_id" class="user_name" >
I want to add the string after '-' to the text box.For eg: for Dr. Prashant Salunke-dr.salunke .I must only have dr.salunke in the text box as it is after '-'. I have written some code given below:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".slidingDiv").hide();
$(".show_hide").show();
$('.show_hide').click(function(){
$(".slidingDiv").slideToggle();
});
$('#list li').click(function()
{
var arr=$(this).text().split('-');
$('#user_id').val($(arr[1]).text());
});
});
</script>
<a href="#" class="show_hide">List of doctors</a><br />
<div class="slidingDiv">
<ul id="list">
<li style="cursor:hand;">Dr.Prashant Salunke - dr.salunke.</li>
<li style="cursor:hand;">Dr.Kalam - dr.apj</li>
<li style="cursor:hand;">Dr.Manmohan Singh - dr.economics</li>
</ul>
</div>
</div>
But it is not working. Please suggest some solution.
The arr[1]
value does not need to be converted to a jQuery object. You can use it as it is to set the val()
of the input
, although using $.trim
to remove leading/trailing spaces may help:
$('#list li').click(function() {
var arr = $(this).text().split('-');
$('#user_id').val($.trim(arr[1]));
});
You can do this:
$('#list li').click(function () {
var arr = $(this).text().split('-');
$('#user_id').val(arr[1]);
// arr returns ["Dr.Prashant Salunke ", " dr.salunke."]
// arr[1] returns dr.salunke.
});
As, arr[1]
has already the text value, there is no need to do $(arr[1]).text()
for getting the text value from it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.