Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  6] [ 5]  / answers: 1 / hits: 19415  / 9 Years ago, mon, august 10, 2015, 12:00:00

I was wondering how I would go about adding id's to buttons that I'[m creating in javascript. I am pretty much pulling whatever results match what I'm looking for from parse, and displaying it in an un-ordered list. Im bulding an invitation system so i have also created an accept button underneath. How do I go about associating the accept button with that specific object that was pulled?
Here is my code:



    function checkInvites(){
var invite = Parse.Object.extend(sessions);
var query = new Parse.Query(invite);
query.equalTo(user, bubbba);
query.find({
success: function(results) {
console.log(Successfully retrieved + results.length + scores.);
// Do something with the returned Parse.Object values
for (var i = 0; i < results.length; i++) {
var object = results[i];
var items = document.getElementById(invites);
var item = document.createElement(li);
item.innerHTML = object.get('user')+' '+ object.id;
items.appendChild(item);
var btn = document.createElement(BUTTON); // Create a <button> element
var t = document.createTextNode(accept); // Create a text node
btn.appendChild(t); // Append the text to <button>
items.appendChild(btn);
}
},
error: function(error) {
alert(Error: + error.code + + error.message);
}
});
}

More From » javascript

 Answers
38

You could use this:



var btn = document.createElement(BUTTON); 
btn.id ='someId';


Please keep in mind that id should be an unique attribute, although you are not forced to make it unique. But if you don't plan to make it unique, better use a class.






If you are going to use jQuery, you could do this instead:



$('<button>accept</button>').attr('id', 'someId');


Explanation:




  • $('<button>accept</button>'): creates an element as specified in string - a button with accept text

  • .attr('id', 'someId'): set an attribute of element, this will add id=someId to button

  • resulting element's html looks like this: <button id=someId>accept</button>


[#65457] Saturday, August 8, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaelyn

Total Points: 619
Total Questions: 102
Total Answers: 104

Location: Honduras
Member since Sun, Dec 26, 2021
2 Years ago
;