2

I have the following jquery snippet which looks for the div with the classes "selector-options my-wrapper selected"

<div class="selector-options my-wrapper selected">
    <a class="my_anchor" title="This is my link title" href="www.myexample.com">
        My Link
    </a>
</div>

jQuery(window).load(function()
    {
        jQuery( ".elector-options.my-wrapper.selected" ).css( "border", "1px solid blue" );
    });

This is working great but now I would like to save the .my_anchor link title attribute within the specific div as a variable.

Anyone got a example of something similar I can see?

3 Answers 3

3

If the anchor element is the only one with that class, this should work:

var title = $('.selector-options.my-wrapper.selected > .my_anchor').attr('title');

If it's not the only one, you should use an id.

This assumes the anchor is a direct child of .selector-options.my-wrapper.selected

Sign up to request clarification or add additional context in comments.

Comments

0

You can try something like this:

var title = "";
$(".my_anchor").on("click", function(e){
  title = $(this).attr("title");
  alert(title);
  return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="selector-options my-wrapper selected">
    <a class="my_anchor" title="This is my link title" href="www.myexample.com">
        My Link
    </a>
</div>

1 Comment

This will work but not sure if OP needs the value before a click event or not
0

Try this:

var myVar = $('.selector-options.my-wrapper.selected').find('.my_anchor').attr('title');

4 Comments

I am looking to target the specific .my_anchor within the div. Will make OP clearer
I don't understand, are there multiple .my_anchor elements? if so how do you know which one you're trying to target?
There isn't multiple .my_anchor elements in the example I have given but I am looking to target the .my_anchor within the given div
class selector always return arry

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.