2

I am using label for attribute for input elements in my website that will help blind users.

Currently when user click on label, the corresponding input is getting activated. That means if there is textbox for name, then cursor will go in the start of textbox.

For example, if name in textbox is "John", then on click label, cursor will enter in textbox and will show before "John".

But what I want is that it should select "John". That means text of textbox should be selected.

Can anyone help me how I can implement this?

My code is shown below:

<div class="editor-label"> <label for="ContactName">*Name</label> </div> <div class="editor-field"> <div> <input id="ContactName" maxLength="40" name="ContactName" type="text" value="John" /> </div> </div>

1 Answer 1

3

I am unsure if you can achieve this by just using html/css, so it's very likely that you need to use a JS lib, such as jQuery.

By using jQuery, you can use the select() method when the label is clicked, using something like this;

$(function() { $('label').click(function() { var id = $(this).attr('for'); $('#'+id).select(); }); }); 

A working example can be found here: http://jsfiddle.net/sf3bgwxr/

Sign up to request clarification or add additional context in comments.

6 Comments

Wow thats weird.... we posted the exact same answer lol. I will delete mine.. you were quicker on the draw
Select is an event which will fire on selecting text.I dont think it will be used to select text..@MrMarlow
Thank you for your reply. But I want to prefer CSS, if possible?
@MrMarlow. Thank you dude. Finally I fixed using your solution. Now one another issue. Can u please look for this: stackoverflow.com/questions/31203636/…
@Jayababu - I provided a jsFiddle to show it working. It does work for this.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.