lofa in
lofa in

Reputation: 317

How to pass the string from primefaces to the javascript function?

In primefaces ,how can I change the first letter of a word in Capital automatically on blur? If I am using the following javaScript function , how can I pass the string from primefaces to that JS function?

<script type="text/javascript"> 
                    function changeCase(string)
{
                        alert(string);
    return string.charAt(0).toUpperCase() + string.slice(1);
}

Upvotes: 2

Views: 1598

Answers (1)

BalusC
BalusC

Reputation: 1109625

I assume that you're using <h:inputText> or <p:inputText>. They generate a HTML <input type="text"> element which has a HTML DOM property value. You can get the HTML DOM element in the function by just passing this into the JS function.

E.g.

<h:inputText onblur="changeCase(this)" />

with

function changeCase(element) {
    var oldValue = element.value;
    var newValue = oldValue.charAt(0).toUpperCase() + oldValue.substring(1);
    element.value = newValue;
}

As a completely different and more robust alternative, you can also use a Converter which runs then in the server side.

Upvotes: 1

Related Questions