How can I put a placeholder in a struts textfield tag?
Asked Answered
B

6

18

i got error because am using placeholder attribute in struts tags....

<html:text property="name" styleClass="form-control" placeholder="some text"/>

how can resolve the problem,pls help me.

Thanks in Advance.

Bergen answered 26/11, 2013 at 5:51 Comment(4)
There is no placeholder tag in struts.use HTML5 input type tag instead.Suave
You are write @PurnenduDekow
@KannanPriya You can use simple HTML. No need to add anything extra.Dariusdarjeeling
Alternatively a tooltip might be a solution as well. <html:text property="name" styleClass="form-control" title="some text"/>Prude
E
15

Use jQuery attr like below:

<html:text property="name" styleClass="form-control" styleId="abc" />

JavaScript code:

$(function() {
    $("#abc").attr("placeholder", "some text");
});
Effie answered 26/11, 2013 at 6:24 Comment(0)
D
9

Just replace:

<html:text property="name" styleClass="form-control" placeholder="some text" />

With:

<input type="text" name="property" class="form-control" placeholder="some text"
                             │
                             └─── Form property ────┐
                                                    │
       value="<bean:write name="name" property="property" />" />
                                  │                
               Name of form-bean ─┘                

The value of the attribute name must match the property of your form to trip in the request.

Dariusdarjeeling answered 26/11, 2013 at 19:52 Comment(1)
Replacing the struts html tag with normal html tag will result in Struts provided default functionalities(i18n and many more) will be lost.Tactile
E
2

There is a placeholder attribute in struts tags as well called placeholder

<s:form action="Welcome">
  <s:textfield name="username" label="Username" placeholder="Enter Your Name" />
  <s:password name="password" label="Password" placeholder="Password"/>
  <s:submit/>
</s:form>

Edit

There isn't an attribute with the name placeholder (sorry for the confusion), but if you type in placeholder like my code sample, the struts form will be evaluated as below

<form id="Welcome" name="Welcome" action="/User/Welcome.action" method="post">
  <input type="text" name="username" value="" id="Welcome_username" placeholder="Enter your Name">
  <input type="password" name="password" id="Welcome_password" placeholder="Password">
</form>

If you notice, the placeholder attribute renders up just fine with the complete form

Ersatz answered 26/11, 2013 at 6:1 Comment(0)
E
1

You can try with jQuery to add this attribute when document gets ready:

<html:text property="name" styleClass="form-control" styleId="xyz" />

then try adding this jquery:

$(function(){
    $(':input').each(function(){
       $(this).attr("placeholder", this.id);
    });
});

or this answer could help you.

Evenson answered 26/11, 2013 at 6:21 Comment(0)
A
0

A simple alternative using javascript event is as follows :

<html:text property="username" value="Username" onclick="this.value=''" />
Astigmatic answered 9/9, 2014 at 6:53 Comment(0)
T
0

Similar to something written above in jQuery, you can try with javascript :

<html:text property="name" styleClass="form-control" styleId="myId" />
<script type="text/javascript">
    document.getElementById('myId').setAttribute("placeholder", "my personal placeholder");
</script>   
Tennyson answered 24/11, 2023 at 11:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.