1

Somewhere I've made a mistake in the btn.attr() but can't find it. Or apparently it doesn't work like I thought it would,so now I'm confused. I could do it in javascript with "with" but I'd love to learn how to do it in jquery also. So here is the code:

function setattr(id, value) {
    var btn = document.createElement('input');
    btn.setAttribute('type', 'button');
    btn.attr({
        id: id,
        class: "btnclass", 
        value: "value"
        });};

Could I have also used $(this) somehow?

3
  • 1
    You misses a " here value: "value.. Commented May 24, 2013 at 9:24
  • "value" <-- you need one more double quote. Commented May 24, 2013 at 9:25
  • Yeah still it doesn't work. Check my comment on the correct answer Commented May 24, 2013 at 9:30

4 Answers 4

7

That's the right way but

  • you have a syntax problem (string "value" not closed)
  • btn is a DOM element, not a jQuery one
  • class is a reserved word

Do this :

$(btn).attr({
    id: id,
    "class": "btnclass", 
    value: "value"
});

Note that those errors should have been spotted by looking at your browser's console.

Sign up to request clarification or add additional context in comments.

2 Comments

That's excellent. That's the answer I was expecting. Well class was the problem. Value I added it here in stackoverflow. It wasn't in the original code and I also tried $(btn) but obviously class the error. It's a greasemonkey script so there is no debugging unfortunately :( Thank you very much. I'll accept your answer in 4 mins :)
Actually "class" is not necessary. I just checked it without " " and it works fine. :/ $(btn) must have been the problem. That's weird. I should have done something else wrong when I was trying it.
0

Rather than:

var btn = document.createElement('input');
...

You can simply:

var btn = $('<input type="button" id="'+id+'" class="btnclass" value="value"/>');

You can then assign this to your page using .appendTo(), .prependTo(), or various other methods jQuery provides.

JSFiddle example.

1 Comment

Cleaner and readable? You can always separate the attributes into an object as Palash Mondal's answer below does if you want it to be "cleaner and readable". It's basically the same thing though.
0

Using jQuery you can do this:

function setattr(id, value) {
    var $btn = $('<input/>', {
        id: id,
        class: "btnclass",
        type: 'button',
        value: value
    });
}

Please, don't forget to append the new btn to an element.

Comments

0

try this Demo

function setattr(id, value) {
var btn = document.createElement('input');

 $(btn).attr({
    type: 'button',
    id: id,
    class: "btnclass", 
    value: value
});
//bind event
$(btn).on('click', function(){
  alert('work....');
});
  $('body').append(btn);
};

2 Comments

bind() is deprecated, you should use on() instead.
@JamesDonnelly yes, I have changed :)

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.