To change the ID you can change the property of the DOM object or the attribute of the HTML element. These are different things, I explained more about this here in this other answer.
Change property:
(You mentioned this way in the answer, and it’s correct. It’s one of the ways)
// JavaScript nativo
document.getElementById('form').id = 'form_form_modulo';
// jQuery:
$('#form').prop('id', 'form_form_modulo');
Change attribute:
// JavaScript nativo
document.getElementById('form').setAttribute('id', 'form_form_modulo');
// jQuery:
$('#form').attr('id', 'form_form_modulo'):
Both do what you want, but changing only the property will not be visible in HTML when inspecting with dev tools.
I read in the comments you tried to
function SalvarBotoes() {
alert($('#form').attr('id'));
$('#form').attr('id', 'form_modulo');
alert($('#form').attr('id'));
form.submit();
}
The reason this give Undefined is because the last call to $('#form')
It will no longer give anything exactly because you changed the ID and this selector no longer finds any element with this ID. You would have to have:
function SalvarBotoes() {
var $form = $('#form');
alert($form.attr('id'));
$form.attr('id', 'form_modulo');
alert($form.attr('id'));
$form[0].submit();
}
@Andersoncarloswoss , It was time to type here wrong writing, I already updated the question. Obg
– Gleyson Silva
What is the need to change the value of
id
of an element? And no use looking at the source code of the page. It does not change. You need to look at the DOM in the browser development tools.– Woss