user1434177
user1434177

Reputation: 1977

Set element id = javascript function

So I have an issue that I'm not 100% sure on how to solve. Im looping through a list of strings and creating multiple forms, what I want to do is set the id of the elements to the string minus spaces. I have a javascript function to remove the spaces

function trimWhiteSpaces(name) {
return name.replace(/\s+/g, '');
}

and below is my current code for generating the forms:

@(name: List[String])

...

@for(name <- names) {
        <td>@name
        @form(routes.Application.makeCall()) {
        <div id="hiddenForm" style="visibility:hidden">
            <input type="text" name="commandID" id="id@name" value="10" />
            <input type="text" name="source" id="sourceCall@name" value="source" />
            <input type="text" name="destination" id="dest@name" value="@name" />
        </div>
        <input type="submit" value="Call" id="call@name"/>
        }
}

but what I would like to do is: is something like:

<input type="submit" value="Call" id="call{js:timWhiteSpaces(@name)}"/>

Any help would be appreciated. The other option I have thought that would work would be instead of passing strings pass a new custom objects that has two options. But as I'm not an expert at html/javascript i was wondering if there was a way to do it in javascript?

Upvotes: 1

Views: 974

Answers (1)

Samuel
Samuel

Reputation: 1667

I would suggest you do it on the server side, as in

id="@(name.replace(" ", ""))"

Upvotes: 3

Related Questions