Get button by class

this should be a simple addition of an attribute on click, but am having issues w/it. Could i get some advice please? thx.

var btn = $('.button .right .fill');

$(btn).click(
	function(){
		$(this).data(('data-whatever=\"'+ item +'\"').data());
	}
);

or would this be more reasonable, w/this pointing to the button of course.

$(this).bind("click", function{
	$(this).attr('data-whatever=\"'+ item +'\"');
});

jQuery’s attr method doesn’t accept a string in the format ‘attr=“value”’, it requires two arguments or a hash, either of these work.

$(this).attr('data-whatever', item);
$(this).attr({ 'data-whatever': item });

You can check the api here:
http://api.jquery.com/attr/#attr-attributes

Thank you Mark will try that.
D

No problem

var btn = $(‘.button .right .fill’);
$(btn).click

You don’t need to wrap btn in the $ function here on the second line, it’s already a jQuery wrapped element returned from the $ above. Most people name these variables starting with a $ so they know which ones they are.

var $btn = $('.button .right .fill');
$btn.click
2 Likes

Thanks Mark for the info… :slight_smile:

@Shay_Fox
I’d like to see how this has helped you, To satisfy my curiosity and so others might benefit, please post the code you were having problems with and how you used the above to solve it.

Returning to this topic:

$dw = $("a [href*="deliverItem_"]");

This should work correct? am having some issues w/the code . but from what I know and have read the code above is declaring a variable for all a tags that contain deliverItem_.
Even thought dynamically the deliverItem will be deliverItem_1 or deliverItem_100.

I tested it in the console with

$("a [href*="deliverItem_"]"); 

and got SyntaxError: missing ) after argument list

Just a wild guess but shouldn’t it be.

$("a [href*=" + deliverItem_ + "]");

Hi paul, no in this case i am targetting a button with id=deliverItem_# w/the number changing w/each item.
I have this type markup.

<div id="download_item_#">
	<a tag>
</div>

so the item itself is a variable that changes with each diff item.
what I am trying to do is to get all the child a tag of a div whose id starts w/ deliveItem_
and once selected add an attribute to them by way of jquery.

Script reads
“see a quotation mark, string to follow”
“see another quotation mark, that’s the end of the string”
script chokes “huh, what’s this?”

try
$('a [href*="deliverItem_"]');

I have also done this that gets me closer to my intent.

$("a[href*=\"javascript://\"]");

As this code is calling js from another location. but it grabs to many of the links on the page. So alternatively I am trying to narrow it down but by selecting the parent or grandparent of that a tag

yah sorry if I have missed any " or ’

I was also toying w/this as I just read about regEx or asciPuke.

$ds = /^download_button_\d$/;
$dw = $("a[href*=\"javascript://\"]");
$df = ds.dw;  //have not figured out this part yet. $(ds > dw)...?

And sorry for dragging on but reading and trying diff things as I go along.
the code below was run in the console seems to return me an empty object. Object

$( "div[class^='.button']")  //a
or
$( "div[class^='.button .right'] > a[href*=\"javascript://\"] " )  //b
$(".button .right")  /c

Any advice on how I am not getting this?

last comment. (if anyone one wants to give advice on the aforementioned code great) I think I’ll be using this.

$dc = $( "a[onclick^='downloadItem']");  //the a tag has an onclick method.
$dc.attr({'data': + asset});

we’ll see if it works. Right now cache issue make it hard to tell

Please provide a complete working example when asking for help, each example you posted has different code than the previous and it’s impossible to know what you’re asking for specifically.

The last example you posted has correct code if you are trying to select links that have an onclick attribute that starts with the value “download_item_”

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title></title>
</head>
<body>
<a href="example.pdf" onclick="download_item_1()">Download</a>
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script type="text/javascript">
  var $dw = $('a[onclick^="download_item_"]');
  console.log($dw);
</script>
</body>
</html>

@markbrown4 Yep sorry. Actually was thinking that everyone but me had given up on the thread and I was just mumbling out loud.
Can’t even check if the code works yet as the cache still has not cleared.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.