0

I have the following code:

 <h2> <c:out value="${Name}"/> </h2>
 <img id="deleteSpecialCharacters('screenshot0<c:out value="${Name}"/>Preview')" class="screenshotImg" src="#" alt="screenshot"/>

With the javascript function:

function deleteSpecialCharacters(String) {
        var NewString = String;
        NewString =  NewString.replace(/[^A-Z0-9]+/ig, "_");
        return NewString;
    }

Since the Java string contains special characters, i want to filter them before its used inside the ID. But since the Javascript function is inside the ID tag, it sees the function as a String.

So how do i call the javascript function inside the id filtering the Java code

7
  • why not filter the variable in java directly, why use javascript? Commented Dec 8, 2016 at 9:58
  • Because i need the variable including the special characters on the same page Commented Dec 8, 2016 at 10:00
  • js variable or java? Commented Dec 8, 2016 at 10:03
  • Java, I print the java variable first, and then put that name without special characters inside the id. Commented Dec 8, 2016 at 10:07
  • can't you have 2 java variable one with special character one without?? Commented Dec 8, 2016 at 10:09

3 Answers 3

1

You cannot execute JavaScript functions this way. You have two options: you either do it on the server side using java and generate the id id="${...}" or you do it on the client side on the onload event.

Sign up to request clarification or add additional context in comments.

1 Comment

The first one isn't an option for me since the code is inside an foreach loop. I'll try the second one thanks
0

You could try calling the js function before the HTML page loads.

You could try writing the javascript function deleteSpecialCharacters just after the body.

<body>
<script type="text/javascript">
    your js function
</script>

Hope this helps!!

Comments

0
<h2> <c:out value="${Name}"/> </h2>
<img class="screenshotImg" src="#" alt="screenshot"/>
<input type="hidden" id="replacement_name" value='screenshot0<c:out value="${Name}"/>Preview'/>

Then read value of hidden field and pass it to javascript method and assign resulting value as id to img element.

<script>
   function deleteSpecialCharacters(String) {
      var NewString = String;
      NewString =  NewString.replace(/[^A-Z0-9]+/ig, "_");
      return NewString;
    }
   $(document).ready(function() {
      var cleanStr = deleteSpecialCharacters($('#replacement_name').val());
      $('.screenshotImg').attr('id', cleanStr);
   });
</script>

This will work only if you are sure that there is going to be only one img element with that class.

The better is solution is to pass actual and cleaned values from server itself or have a look at this thread Regular expression to remove special characters in JSTL tags.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.