[solved] jquery get text up until first space in form field

All about creating websites!
Post Reply
User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

[solved] jquery get text up until first space in form field

Post by maboroshi »

I was wondering if anyone could give some advice on getting all the text in a form input:text field up until the first whitespace character

Basically I am inputting an address like so 5575 Fake street New York USA and I need to get the 5575 from the form and add it to another form

Any ideas

*cheers

Maboroshi

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: jquery get text up until first space in form field

Post by ayu »

Try this

Code: Select all

<!DOCTYPE html>
<html>
<head>
  <style>

  p { color:blue; margin:8px; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <input type="text" value="some text"/>
  <p></p>
<script>
    $("input").keyup(function () {
      var value = $(this).val();
      var substr = value.split(" ");
      $("p").text(substr[0]);
    }).keyup();
</script>

</body>
</html>
Input the text you posted

Code: Select all

5575 Fake street New York USA
"The best place to hide a tree, is in a forest"

User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

Re: jquery get text up until first space in form field

Post by maboroshi »

Thanks Cats! Worked great I ended up using

Code: Select all

<script>
    $("#address").keyup(function () {
      var value = $(this).val();
      var substr = value.split(" ");
      $("#listing_address_number").val(substr[0]);
    }).keyup();
</script>
*cheers

Post Reply