简体   繁体   中英

Swap input placeholder text with span text jquery

Well i have problem as in how do i swap a input placeholder text with a span text ? ...

    <div class="form-inputs">
      <div class="control-group string required user_name error error_state">
        <label class="string required control-label" for="user_name"><abbr title=
          "required">*</abbr> Name</label>
          <div class="controls">
            <div class="input-prepend">
              <input class="string required" id="user_name" maxlength="100" name=
              "user[name]" pattern="^[^0-9`!@#\$%\^&amp;*+_=]+$" placeholder=
              "Full Name" size="50" type="text" value="" />
            </div>
            <span class="help-inline">can't be blank</span>
          </div>
      </div>

        <div class="control-group email required user_email error input-error">
          <label class="email required control-label" for="user_email"><abbr title=
            "required">*</abbr> Email</label>
            <div class="controls">
              <div class="input-prepend">
                <input class="string email required" id="user_email" maxlength="255"
                name="user[email]" pattern="\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z" placeholder=
                "Email" size="50" type="text" value="" />
              </div>
              <span class="help-inline">can't be blank</span>
            </div>
        </div>

          <div class="control-group password required user_password error input-error">
            <label class="password required control-label" for=
            "user_password"><abbr title="required">*</abbr> Password</label>

            <div class="controls">
              <div class="input-prepend">
                <input class="password optional" id="user_password" maxlength="128" name=
                "user[password]" placeholder="Password" size="50" type="password" />
              </div>
              <span class="help-inline">can't be blank</span>
            </div>
          </div>
        </div>

My rails code adds the span with the class "help-inline" if there exists a error on submission ...this span is not added to the input if there are no errors ...

How do i swap the input placeholder text with the span text ? ... for each input in the form ...

eg ... i want to change the placeholder text "Full Name" to "can't be blank" if there was an error in the first field ...

Thanks very much ...

Try this -

$('#user_name').prop('placeholder', function () {
    return $(this).parent().next('.help-inline').text()
});

Well this is the thing which worked :

Javascript :

$("input").each(function() {
  if ($(this).parent().next(".help-inline").text().length) {
    $(this).prop("placeholder", $(this).parent().next(".help-inline").text());
    return $(this).val("");
  }
});

Coffeescript :

$("input").each ->
    if $(this).parent().next(".help-inline").text().length
      $(this).prop "placeholder", $(this).parent().next(".help-inline").text()
      $(this).val ""

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM