webster
webster

Reputation: 4032

Changing value of an attribute using jQuery

I am having a file input field:

<input class="uploadReptCtrl" id="lang_file_1" name="photo[[1,312,3]]" type="file">

I am trying to change the third element of the array in the name attribute so that it becomes:

<input class="uploadReptCtrl" id="lang_file_1" name="photo[[1,312,4]]" type="file">

How is it possible?

Upvotes: 0

Views: 42

Answers (1)

Newinjava
Newinjava

Reputation: 960

Try this

$('#lang_file_1').attr('name','photo[[1,312,4]]')

https://jsfiddle.net/jzzL1d2r/

you can store the value in a variable and set it as

var a = 4;
$('#lang_file_1').attr('name','photo[[1,312,'+a+']]')

https://jsfiddle.net/jzzL1d2r/1/

As you want to get the 3rd number increment it and assign it again you can do following

var numberPattern = /\d+/g;
var string = $('#lang_file_1').attr('name');
var a = string.match( numberPattern );

$('#lang_file_1').attr('name','photo[[1,312,'+(++a[2])+']]');

https://jsfiddle.net/jzzL1d2r/3/

Upvotes: 1

Related Questions