1

I have a requirement where I need to set html input tags ids with js variables.

example:

<input type='text' id='(here I need js variable)'/>

I know there's a way to do that by creating entire element by

document.createElement('div name');

and I can append to the div, but I want it in a simple way.

If it is a Java variable then I can do it like

<%String myVar = "txtId"%>
<input type='text' id='<%= myVar%>'/>

Is there any simplest way to put a js variable in a html tag?

Update :

Thanks for the answers guys, I know we can create any number of input elements and append to the parent element. And also we could get an element by tag name and can set id for those elements.But my question is, is there a way to use(or) place (or) insert a js variable in html tag just like we could insert a java variable in the html tag as I've shown above.

like,

<script>
var txtId = 'txt1';
</script>
<html>
<input type='text' id='document.write(txtId)'/>
</html>

Is there a way to do like this?

1
  • Dynamic id's from server-side are most appropriate for the most dynamical stuff. Can you further elobrate your requirement? How your html elements are getting generated? Commented Dec 18, 2013 at 6:00

4 Answers 4

1

Assign ids to exist element , if you want to use java variable as your new id, you can use jstl tag like this

var inputs = document.getElementsByTagName("input");
for(var i = 0 ; i<inputs.length ; i++){
    inputs[i].id = "newIdWhatYouWant";
}
<input type='text' class="name"/>
Sign up to request clarification or add additional context in comments.

Comments

0

you can use attr() jQuery function and set any string you like. so in action:

    var str="newId"
    $('name_or_#id_or_.class').attr('id',str)

2 Comments

I don't want to create an element but to set the id of the html tag itself by js variable.
so when did i ask for crating a variable??
0

Can you use something like this ?

<script>
    var a = "some_id";
    document.write("<input type='text' id='"+a+"' />");
</script>

http://jsfiddle.net/9DAE4/

Comments

0

I think this will help you.

If you face any problem then tell me.

OR

You can create whole control dynamically and set all data see this link. It's very helpful.

I have tried this one, you will have existing ID then only you can change it's ID :

<script type="text/javascript"> 
 function addName()
 {
     var k ='add';

    document.getElementById('txttt').id = k;
    //document.getElementById('two').value = 'hi'

    var srchdata = document.getElementById(k).id;    
    alert(srchdata);
 } 
</script>

<body onload="addName()">

If your data is dynamic then some problem can be arise. I have used alert to display new ID of textbox.

If you want some other way then tell me how ? And what problem can be arise in this ?

5 Comments

Would you want something like <input type='text' id='txt1'/> and next time it will give <input type='text' id='txt2'/> ? or something else ? Give me some idea..
Assume I have a js variable "var fld = 'txt1'" in a js file and I want to use that js variable in html tag like: <input type='text' id=fld/>, is there a way to do it? I know we could use document.getElementByTagName('input'); but other than this is there a way to put js variable in html tag?
See Update In Answer.
Thanks for the answer, I made an update in my question, please check it.
Ok I ll try to do something like that, when it completes I ll post to you.

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.