-1

I have a html code containing input field as below:

<input id="'+count+'" name="quan'+obj+'" type="button" data-quantity="1"/>

Whereas, id and name of input field are dynamic as shown above because id count and name obj are dynamic. (Ranges from 1-5)

That means at certain time, the above input field could be:

<input id="4" name="quan2" type="button" data-quantity="1"/>

Now, What I want to do is to get the data-quantity value of input field when my name of input field is quan3 OR quan(N) Whatever (N) would be.

data-quantity also contains dynamic value.

funcation get()
{
   count=3;
   obj=2;
   // What to do here to get the value of data-quantity???????
}

Note: I have also tried this below but doesn't worked.

$(".quan" + obj).attr("data-quantity","4"); // NOT WORKED
$("#" + count).attr("data-quantity","4"); // NOT WORKED
kamranbhatti585
  • 232
  • 2
  • 16

2 Answers2

1

You tried .quan, but the . indicates a class and you tried #, but that indicates id. To query for an attribute, you need [] as shown below. Also ^= with an attribute query means "starts with" so you really don't need to worry about the number value unless it matters to your logic.

var count = 3;
var obj = 2;

// Report the 'data-quantity' attribute value now:
console.log($("input[name^='quan']").attr("data-quantity"));

// Locate all elements that have a `name` attribute that 
// has a value that starts with 'quan' and set their 'data-quantity'
// attribute value to "4".
$("input[name^='quan']").attr("data-quantity","4"); 

// Report the 'data-quantity' attribute value now:
console.log($("input[name^='quan']").attr("data-quantity"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="4" name="quan2" type="button" data-quantity="1">
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • No, I have already explained in my question that the **name** is dynamic. I mean there could be 5 input fields with name="quan1" & name="quan2" & name="quan3" ..... name="quan4" – kamranbhatti585 Jan 05 '18 at 22:59
  • @kamranbhatti585 Yes, I understand that. You also said you wanted to get a reference to the input regardless of what the number was on the end of `quan` and this code does that. – Scott Marcus Jan 05 '18 at 23:01
  • Thank you for your time. Above post from another fellow answered my question rightly. – kamranbhatti585 Jan 06 '18 at 00:02
1

Check this one.

You want to get attr value of data-quantity by selecting input with a name that ends with 1,2,3...n.

This is how it can be done.

function getAttrValueFromInput (marker) {
 const attrValue = $(`input[name^='quan${marker}']`).data('quantity');
  
  return attrValue;
}

function setAttrValueFromInput (marker, value) {
$(`input[name^='quan${marker}']`).attr('data-quantity', value);
}

setAttrValueFromInput(2, 2000);


console.log(getAttrValueFromInput(2));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="1" name="quan1" type="button" data-quantity="1"/>
<input id="2" name="quan2" type="button" data-quantity="12"/>
<input id="3" name="quan3" type="button" data-quantity="90"/>
Nikola Mitic
  • 1,298
  • 3
  • 11
  • 23