0

I'm trying to use javascript to automatically modify my input boxes to

<div class="input-group">
**<input type="text" class="form-control " placeholder="Text input">**
<span class="input-group-addon">*</span>
</div>

jsfiddle: http://jsfiddle.net/BEC7N/

The bold line is the line that already exists.

Below is my javascript attempt

$('[data-val-required]').prepend(' <div class="input-group">');
$('[data-val-required]').append(' <span class="input-group-addon">*</span></div>');

But it doesn't work. Any ideas? Thank you.

2 Answers 2

2

Change your code to this:

$('[data-val-required]').wrap(' <div class="input-group"></div>');
$('[data-val-required]').after(' <span class="input-group-addon">*</span>');
$('[data-val-required]').after(' <span class="required-indicator">*</span>');

Working Example

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

5 Comments

This does work. Thank you :) but my extra * is not attached to the original input box. Any ideas? jsfiddle.net/BEC7N/48
Just run your $('[data-val-required]').after(' <span class="required-indicator">*</span>'); after the other two lines. If this is what you mean?
I've updated my comment with a jsfiddle illustrating the problem.
As well you need to add the class form-control to your input if you want to use bootstrap styles.
I solved my design issues by putting the * before the textbox instead of after. Thank you both.
2

Use .wrap()

$('[data-val-required]').wrap('<div class="input-group" />');
$('[data-val-required]').after(' <span class="input-group-addon required-indicator">*</span>');

3 Comments

This does work. Thank you :) but my extra * is not attached to the original input box. Any ideas? jsfiddle.net/BEC7N/48
Can you post desired output?
I solved my design issues by putting the * before the textbox instead of after. Thank you both.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.