0

I'm running a small javascript replace to replace text on a button click and I can't see why it's not working. Could anybody tell me what i'm doing wrong?

http://jsfiddle.net/zg4VB/2/

$("#button").click(function() {
    $("#editme").value = replaceLinks($("#editme").val());
})

function replaceLinks(text) {
    var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
    return text.replace(exp,"[url=$1]$1[/url]"); 
}

3 Answers 3

3

Two issues. First, $("#editme").value = replaceLinks($("#editme").val()); should be $("#editme").val(replaceLinks($("#editme").val()));

Second, your button ID was id="#button" and should be id="button"

jsFiddle example

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

2 Comments

Something so simple! Thank you very much. (Will accept as answer when I can)
writing $(el).val(mutator_func($(el).val)) is wasteful and repetitious. jQuery allows you to just write $(el).val(mutator_func), per my answer.
2

.value is usually the direct property of a DOM Input element. The jQuery function to access or change an element's value is .val()

Usefully, .val() allows you to pass a function to mutate the current value. That function is passed the element's index and current value, and the function's return value replaces the original value. All you need to do is change the signature of your replaceLinks function to allow for the index parameter (which you can then safely ignore).

$("#button").click(function() {
    $('#editme').val(replaceLinks);  // calls `replaceLinks` to mutate its value
});

function replaceLinks(index, text) {
    ...
}

See http://jsfiddle.net/alnitak/XzrHP/

4 Comments

In the way he is using it above it would be a direct property of the jQuery collection
@ExplosionPills it would, if there were such a property.
By performing the assignment you create the property. Of course you wouldn't be able to retrieve it since the reference to the created object is lost immediately, but in theory you could. Whatever it is, it's not a property of the DOM node.
You're picking nits - all I was trying to say was that he was clearly trying to use the .value DOM property on a jQuery collection - a common mistake.
1

A couple problems: .value = should be .val(. It's used both to set and get the value. You also had the button ID as #button, but it should just be button.

You could also do $("#editme").val(replaceLinks); if you change the arguments of replaceLinks to function replaceLinks(_, text) {

http://jsfiddle.net/ExplosionPIlls/zg4VB/5/

$("#button").click(function() {
    $("#editme").val(replaceLinks);
})

function replaceLinks(_, text) {
    var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
    return text.replace(exp,"[url=$1]$1[/url]"); 
}

If you want to use replaceLinks on strings as well, you could check arguments in replaceLinks as well: text = arguments[arguments.length - 1]

1 Comment

_ for unused parameters - Erlang programmer?

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.