9

Can I rename a field of my form using Javascript? Like changing:

<input type="text" name="chicken" id="chicken"/>

to

<input type="text" name="horse" id="horse"/>
2
  • What have you tried so far? Did you read any documents on DOM-manipulation? Commented May 13, 2013 at 11:39
  • How about removing and adding new element to the dom object. Commented May 13, 2013 at 11:39

5 Answers 5

13

Of course it is possible:

var field = document.getElementById("chicken");
field.id = "horse";  // using element properties
field.setAttribute("name", "horse");  // using .setAttribute() method
Sign up to request clarification or add additional context in comments.

1 Comment

i don't think .id renames the object. apparently it changes the id.
10

You can change the name attribute, but in my opinion, you shouldn't change the id.

You can use :

document.getElementById("chicken").setAttribute('name', 'horse');

Edit

The following statement is purely subjective, and might not have some room on SO.

From W3.org :

The id attribute has several roles in HTML:

  • As a style sheet selector.
  • As a target anchor for hypertext links.
  • As a means to reference a particular element from a script.
  • As the name of a declared OBJECT element.
  • For general purpose processing by user agents (e.g. for identifying fields when extracting data from HTML pages into a database,
    translating HTML documents into other formats, etc.)

As I see it, the ID is an unique identifier which is really useful to retrieve an element from a script (among other things), and I personally want to be able to retrieve this single element with the same selector whatever manipulation it endures during runtime. So, I won't change it.

Example : if I transform a chicken into a horse, I keep in mind that it used to be a chicken before, and I'd rather remember it as a mutant which I'll select with $('#chicken[name=horse]'), and fake it as an horse using others CSS selectors.

2 Comments

It's perfectly possible to change the id of an element.
You have a typo in the name of .setAttribute() method.
4

Yes, it possible to change any data at runtime using js :)

var elementToRename = document.getElementById("chicken");
elementToRename.id = "horse"; 
elementToRename.setAttribute("name","horse"); = "horse";

Comments

2

Once you have access to the element object (i.e. with getElementById) you can just change its name property:

el.name = 'horse';

The attribute will mirror the value of the property.

Comments

-1

how to rename a tab in JavaScript. Below is the code.

var i=parseInt(1);
        var clickid;
       $(document).ready(function(){
       	$("#demoTabs").tabs();
        $(".nav-tabs").on("click", "a", function(e){
        e.preventDefault();
      // $(this).tabs("show");
    })
    .on("click", ".close", function () {
    	var w_demo=$('#demoTabs').outerWidth();
        $(this).parent().remove();
        $("#demoTabs").width((w_demo-105)+"px");
	});
    $('.add-tab').click(function(e) {
        e.preventDefault();
        var html="<li class=\"tabs\" id="+i+"><a href=\"#\">New Tab</a><span class=\"close\">x</span></li>";
        $(this).closest('li').before(html);
		var w=$("#demoTabs").outerWidth();	
        var w=w+95;
        document.getElementById('demoTabs').style.width=w+"px";
        i=i+1;
	});
    if (document.getElementById('demoTabs').addEventListener) {
    document.getElementById('demoTabs').addEventListener('contextmenu', function(e) {
            $("#rmenu").toggleClass("hide");
            $("#rmenu").css(
              {
                position: "absolute",
                top: e.pageY,
                left: e.pageX
              }
            );
            e.preventDefault();
            clickid=$(this).id;
            prompt(this+" "+clickid);
     }, false);
}

// this is from another SO post...  
$(document).bind("click", function(event) {
  document.getElementById("rmenu").className = "hide";
});
$("#refresh").click(function(){
    location.reload();
})
$("#rename").click(function(e){
var reply=prompt("type in a name for tab");
if(reply!=null && reply.trim().length>0)
{
     // $(e.target).prev("tabs").text(reply);
       // prompt(clickid);
      document.getElementByClass("").innerHTML=reply;
}
});
     });
   #body{
  height:22.8px;
  width:32.4px;
}
#demoTabs{ 
	width:90px;
    overflow: auto;
}
.nav-tabs > li > span {
    float:right;
    cursor:pointer;
    position:absolute;
    right: 6px;
    top: 8px;
    color: red;
}
.hide {
    display: none;
}

#rmenu {
  border: 1px solid black;
  background-color: white;
  cursor:pointer;
}

#rmenu ul {
  padding: 0;
  list-style: none;
  margin: 0;
}
#rmenu li{
    list-style: none; 
    padding-left: 5px; 
    padding-right: 5px; 
}
<body style="position:relative">
    <div id="demoTabs">
    <ul class="nav nav-tabs">
        <li class="tabs"><a href="#" class="add-tab">+Add</a></li>
    </ul></div>
    <div class="tab-content">
    </div>
    <div class="hide" id="rmenu">
        <ul>
            <li id="refresh">Refresh</li>
            <li id="rename">Rename</li>
        </ul>
    </div>

Comments

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.