1

I've got an element on my page:

<a data-toggle="modal" data-nodeID="" class="quickView" data-target="#productModal" href="#">

And this element gets 'repeated' in using a serialized ajax object and what I'm trying to do is change the data-nodeID attribute but I can't seem to alter it. I've tried finding the element by it's class name:

$(myTemplate).find('.quickView').attr({'data-nodeid': product.Id});

Does anyone know how this is done, please?

Thanks, C

2 Answers 2

2

You should look at jQuery .attr() documentation.

.attr(attributeName, value)

attributeName
Type: String
The name of the attribute to set.

value
Type: String or Number or Null
A value to set for the attribute. If null, the specified attribute will be removed (as in .removeAttr()).


Also be careful about uppercase letters when you try to set your attribute :

$(myTemplate).find('.quickView').attr('data-nodeID', product.Id);
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, turns out it was a mistake in me populating the ID (as it was null) corrected that and all is well. Thanks buddy :)
0

You can also try using jQuery .data():

// $(myContainer).find('.quickView').data('nodeid', product.Id);

$('#myContainer').data('customTag', 2);

console.log($('#myContainer').data('customTag'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="myContainer" data-custom-tag="1"></div>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.