0

Slightly confused by this problem. I have written this function to replace punctuation in input elements:

function punctuationReplace(element){
	var elementToChange = document.getElementById(element);
	elementToChange.value = elementToChange.value.replace(/\"/g, '');
	elementToChange.value = elementToChange.value.replace(/\'/g, '');
	elementToChange.value = elementToChange.value.replace(/&/g, 'and');
	elementToChange.value = elementToChange.value.replace(/</g, '');
	elementToChange.value = elementToChange.value.replace(/>/g, '');
}
<input type="text" id="IPR_FNM1" name="IPR_FNM1" value="" maxlength="30" size="31" placeholder="Forename 1" onblur="punctuationReplace(this)"/>

When I manually run the function in the browser console with the relevant ID as the argument, it works. And when inside the function I add the line alert(element.id);, I get an alert containing the correct element ID. So I am confused as to why the above punctuationReplace function won't work when I call it using onblur="punctuationReplace(this)".

I'm sure there is a simple solution but no idea what it is.

5 Answers 5

3

If you pass in this in that fashion, you already have a reference to the element, so no need to call getElementById.

function punctuationReplace(element){
    var elementToChange = element; //document.getElementById(element);
    elementToChange.value = elementToChange.value.replace(/\"/g, '');
    elementToChange.value = elementToChange.value.replace(/\'/g, '');
    elementToChange.value = elementToChange.value.replace(/&/g, 'and');
    elementToChange.value = elementToChange.value.replace(/</g, '');
    elementToChange.value = elementToChange.value.replace(/>/g, '');
}
Sign up to request clarification or add additional context in comments.

Comments

3

Stop using inline event handlers, and the issue solves itself

document.getElementById('IPR_FNM1').addEventListener('blur', punctuationReplace, false);

function punctuationReplace(){
    this.value = this.value.replace(/("|'|<|>)/g, '').replace(/&/g, 'and')
}

3 Comments

While not using intrinsic event attributes is a good idea, that code won't work unless you also rewrite the punctuationReplace function.
This is a really nice solution. Would it work using document.getElementsByTagName('input')?
@AndyKaufman - It would, but with a for loop, as that gets a nodeList, not a single element
3

this is a reference to a DOM element. It is not a string containing an ID.

You are trying to use it as an argument to getElementById but when it is stringified (to "[Object object]") there isn't a matching ID in the document, so you get null.

You already have the element. You don't need to search the DOM for it.

Remove the line var elementToChange = document.getElementById(element); and change the other references to element.

Comments

0

You don't need to create a var just keep using element. Like this: http://jsfiddle.net/0xuf64zb/1

function punctuationReplace(element){
    element.value = element.value.replace(/\"/g, '');
    element.value = element.value.replace(/\'/g, '');
    element.value = element.value.replace(/&/g, 'and');
    element.value = element.value.replace(/</g, '');
    element.value = element.value.replace(/>/g, '');
}

And indeed stop using inline calls!

Comments

0

this already have an reference to element you want. You don't need to get it again using getElementById. Just put it straight to your variable:

  var elementToChange = element;

1 Comment

Please edit your answer, and explain why this solves the problem.

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.