1

I have input field like:

<input data-checkout="card-number" type="tel" placeholder="card number" autocomplete="off" class="input-control" value="">

How I can set a value of this field with jquery? How to get data-checkout="card-number" and set value ?

4
  • 1
    Did you try $('[data-checkout="card-number"]').val("Your val"); ? Commented Oct 31, 2018 at 13:09
  • Thanks, this works good Commented Oct 31, 2018 at 13:10
  • Awesome, good luck. Commented Oct 31, 2018 at 13:11
  • Posted it as an answer if you want to mark it as accepted answer. Commented Oct 31, 2018 at 13:14

5 Answers 5

2

in jquery you can use any attribute as a selector using this format:

$('[your-attr="attr value"]')

Answering your question this should work:

$('[data-checkout="card-number"]').val("Your val"); 
Sign up to request clarification or add additional context in comments.

Comments

2

It depends on what you wish to access it by. To access the element, you can use, for example:

$('.input-control')  // Finds all elements with this class

or

$('.input-control[data-checkout=card-number]')  // Finds all elements with this class and this data-checkout value

or some other selector, depending on your application. Then, to set the value you can do:

$(/* your selector */).val('some value'); // Sets the value to 'some value'

and to get the value fo data-checkout:

$(/* your selector */).data('checkout');  // Returns 'card-number'

It is somewhat unclear what exactly you are looking for, but hopefully some of this answers your question.

Comments

1

Using jquery you could use the val function:

$(".input-control").val("Some value")

Using javascript:

document.querySelector(".input-control").value="Some value"

You could get the attribute by:

$(".input-control").attr("card-number")

Then you can do

$(".input-control").val($(".input-control").attr("card-number"))

Hope this helps

1 Comment

.attr('card-number') returns the value of card-number="value". Use .data('checkout') do get the value of data-checkout="value"
1

You can use basic Javascript to set the value.

for(i=0; card=document.querySelectorAll("input[data-checkout=card-number]")[i]; i++){
    card.value="sample value";
} 

Comments

0
$('input[data-checkout="card-number"]').val('here') // replace word (here) with the value you want

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.