0

I have this string inside an input file.

<input type="file" data-url="/modules/wizard/upload.php?eid=18000115&amp;type=ico&amp;case=protagonist&amp;id=121001118" value="" class="wizard_image" name="files">


data-url="/modules/wizard/upload.php?eid=18000115&amp;type=ico&amp;case=protagonist&amp;id=121001118"

Now of this string I only want to change the very last param: id=121001118 with something different and not the entire value of the data-url attribute.

How can I do it? The below one will change the entire string that is not what I am looking for.

newBox.find('input.wizard_image').attr('data-url', 'somethingElse');

Thanks for any help

1
  • Have you tried using Regular Expressions? Commented Jul 1, 2013 at 13:29

4 Answers 4

4

You can use a regular expression:

newBox.find('input.wizard_image').attr('data-url', function(i, val) {
    return val.replace(/id=\d+$/, 'id=somethingElse');
});

Passing a function to .attr makes it easy to modify the existing value.

Explanation of the expression:

id= // literally matches "id="
\d+ // matches one or more digits 
$   // matches the end of the line/string
Sign up to request clarification or add additional context in comments.

Comments

0

I would use string functions: substring, replace.

var str = 'data-url="/modules/wizard/upload.php?eid=18000115&amp;type=ico&amp;case=protagonist&amp;id=121001118"';

var id = str.substring(str.indexOf(";id=") + 4);

str = str.replace(id, "something...");

JSFIDDLE

But, a better solution is using regular expressions.

Comments

0
var newID = 123435465;                       // the new Id you'd like to put into the URL
var $el = newBox.find('input.wizard_image'); // take the reference of the element
var oldURL = $el.data('url');                // get the data-url
var newURL = oldURL.replace(/id=[0-9]+/, newID);// replace the id=number pattern by newID
$el.data('url', newURL);                        // set it to a new one by replacing 

2 Comments

Its a good idea to put some description with an answer, rather than just a block of new text.
The code is very well commented, and the comments serves perfectly well as description, IMHO.
0

easiest way using Regular expresion

newBox.find('input.wizard_image').attr('data-url', 
        newBox.find('input.wizard_image').replace(/id\=[0-9]{0,}/gi, "something-else")
);

Comments

Your Answer

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