开发者

Can I set "id" of button to a variable?

开发者 https://www.devze.com 2023-02-19 19:07 出处:网络
I want to set a button\'s id to a variable, so that I can easily get access to changing numbers of buttons:

I want to set a button's id to a variable, so that I can easily get access to changing numbers of buttons:

<script>var test = 1; </script>
<button id=test> click m开发者_JAVA技巧e </button>

but it is does not work. my motivation to do this : I read from a xml file and display part of them using form. and then I have to change the value of them. and the number of items I read from xml is not fixable. so if i want to control them i need give each item a number.


var test=1;
var ele = document.getElementById('test');
ele.id="button_" + test;
ele.value="button " + test;

Just to be clear, when you say

<button id=test/>

or, for that matter, the more standards-compliant

<button id="test"/>

the word test is just a text identifier. It is not interpreted as javascript.

The only place (I know of) where you can put javascript directly into an HTML attribute and it will be interpreted, is an event handler (such as onclick) or in the href of an a tag, after the prefix javascript:.


var button = document.getElementsById("test");

Your var 'button' will contain the DOM object with the id 'test'. You might wanna do this a little more 'dynamic' something like:

var myArray = [];

var oButton = document.getElementsByTagName("button");
var i = 0;
for(i=0;i<oButton.length;i++) {
 if (oButton[i].className == 'likedButton') {
   myArray.push(oButton[i]);
 }
}

This way, you simply need to add "class='likedButton'" to your button and this will add all those button into the array 'myArray'. Next step if you wanna do anything to those button, just go throught the 'myArray' object myArray[0] showing the first object with the class 'likedButton'. I suggested the class attribute so that you can easily find tons of button. If you use the ID attribute, all IDs MUST be different.


What you've done in your code is assign the id value "test" to the element.

It sounds like you're already dealing directly with DOM elements, so you can just assign the value to their id property. Example:

var list, li, index;

// Find a list that already exists on the page
list = document.getElementById('myList');

// Add four items to it
for (index = 0; index < 4; ++index) {
    li = document.createElement('li');
    li.id = "listItem" + index; // listItem0, listItem1, ...
    li.innerHTML = "I'm list item #" + index;
    list.appendChild(li);
}

Live example


I don't think you can directly set the dom objects id to to a javascript variable but you can can change the id using javascript or jQuery I'm not totally clear on what your trying to do. but I htink what your saying is that you want to change id based on the number of buttons to do that use jquery foreach

$('button').each(function(key,value){
    $(this).attr("id","button_"+key)
});


You cannot change it immediatly inside of the Button ID. Have to change it after the Button.

var button3 = "button3"+i
var button4 = "button4"+i
      '<button id="button3" type="submit" class="accpet-button">Accept</button></td><td>'+
      '<button id="button4" class="cancel-button" data-dismiss="modal">Cancel</button></td>'
  +"</tr>");
var ele = document.getElementById('button3');
ele.id="button3"+i;
var ele = document.getElementById('button4');
ele.id="button4"+i;
i++;
document.getElementById(button3).disabled = button1;
document.getElementById(button4).disabled = button2;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号