0

I am sure this is simple, I just dont seem to be able to get my head around it. I am trying to add a value of an attribute on click.

So there is this button

<input domain_id="9" class="home_send_but" value="send" type="button">

And when clicked

$('.home_send_but').click(function(){
    domain_id = $(this).attr('domain_id');
    $('#send_but').attr('domain_id').val(domain_id);
});

It should add the domain_id of 9 to:

<input domain_id="" id="send_but" type="button" value="Send Now">

However, the domain_id attribute for send_but remains as null.

So how do I add a value to an attribute?

And yes, I know I should be using data attributes.

2
  • Is domain_id global because you didn't declare as a variable: var domain_id = $(this).attr('domain_id'); Commented Nov 27, 2015 at 16:56
  • 2
    You should be using data attributes instead of creating your own. Commented Nov 27, 2015 at 16:57

2 Answers 2

4

You can set attributes in jQuery by using the attr() method in this way:

$("#send_but").attr('domain_id',domain_id);

Source: jquery attr

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

1 Comment

Thats great, I almost thought of trying it, should have.
0

https://api.jquery.com/attr/#attr2

Your code should be $('#send_but').attr('domain_id',domain_id);

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.