Mike
Mike

Reputation: 638

Use jquery to alter link attribute

I'm trying to make a page that generates a quote when you click a 'quote' button and then you can tweet the quote using the twitter button. Here is a link to the twitter button.

The twitter button allows you to tweet out specific text that you want by altering the 'data-text' attribute.

I would like to use my 'quote' button to generate a quote and at the same time alter the data-text attribute of my twitter button so I can click the twitter button after and tweet out a specific quote.

So far I have this which is part of my click function:

$("a").prop("data-text", quotes[j]);

Where j is an index of the quote array but I can't seem to get it to work.

EDIT: Here is a working demo: http://codepen.io/michaelaharvey/pen/wMzGJR

Upvotes: 0

Views: 69

Answers (4)

brk
brk

Reputation: 50326

If you check DOM you will not find any a tag, but .data,.attr,.propwill require the element to be present in DOM

FROM THIS LINK

Store arbitrary data associated with the matched elements or return the value at the named data store for the first element in the set of matched elements.

Upvotes: 1

guest271314
guest271314

Reputation: 1

'://platform.twitter.com/widgets.js`` loads aniframeinto.asdf, removing exisitingaelement within.asdf. When attempting to accessaelement withiniframe`

Uncaught SecurityError: Failed to read the 'contentDocument' 
property from 'HTMLIFrameElement':
Sandbox access violation: Blocked a frame at   
"http://stacksnippets.net" from accessing a frame at 
"http://platform.twitter.com".  
Both frames are sandboxed and lack the "allow-same-origin" flag.

logged to console

Upvotes: 1

Alex Bezek
Alex Bezek

Reputation: 487

Im going to be different and suggest using the jquery data method https://api.jquery.com/data/

$("a").data("text", quotes[j]);

Upvotes: 2

Andrew Brooke
Andrew Brooke

Reputation: 12173

You should be able to use .attr

$("a").attr("data-text", quotes[j]);

Upvotes: 1

Related Questions