0

HTML:

<div class="mydiv">
    My Div
    <ul>
       <li><input type="submit" value="first"/></li>
       <li><input type="submit" value="second (blah blah)"/></li>
       <li><input type="submit" value="third . blah blah"/></li>
    </ul>
</div>

How can I retrieve the value of the input and replace any '(' or '.' with a '\n'? The result would appear like:

second
(blah blah)
3
  • When do you want to do that replacement ? on submit ? when the page loads ? Also, do you replace ( with \n (as your text says) or with (\n (as your exemple does) Commented Sep 26, 2011 at 9:29
  • input text boxes may only have one line, AFAIK. Commented Sep 26, 2011 at 9:41
  • Please stop writing "Thank you in advance!! God Bless!!" on all your questions. There is a reason that we keep removing it. Commented Sep 26, 2011 at 9:44

2 Answers 2

1

Wrote this assuming you wanted to keep the separator as your exemple implied, you can see it working here.

$(document).ready(function(){
  $('.mydiv input[type=submit]').each(function() {
    $this = $(this);
    $this.val($this.val().replace(/(\.|\()/, '\n$1'));
  });
});

EDIT: @vzwick: regarding your edit no, it shouldn't be a one-liner, I'm caching $this on purpose.

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

Comments

0

That should do the trick:

$(document).ready(function(){
    $('.mydiv input').each(function(i,e){
        $(e).val($(e).val().replace('(', '\n').replace('.', '\n'))
    });
});

Also, keep in mind, that this might/will cause problems with rendering in some browsers (i.e. WebKit doesn't resize the input).

http://jsfiddle.net/YTD9p/

2 Comments

Regarding your edit on my answer, I reverted it back and added a link to an answer explaining why to you. In the same level of things, both your replace calls should be combined in one.
Thanks man, I learned something today! Also, your approach is definitely the way to go. Cheers!

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.