jovhenni19
jovhenni19

Reputation: 450

Modify `input` value

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)

Upvotes: 0

Views: 116

Answers (2)

Lepidosteus
Lepidosteus

Reputation: 12037

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.

Upvotes: 1

vzwick
vzwick

Reputation: 11044

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/

Upvotes: 0

Related Questions