Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to select data-id and data-action in jQuery

This is a follow-up to this question: Using javascript:function syntax versus jQuery selector to make Ajax calls

How would I select this fragment?

<div data-id="54" data-action="follow-global-id" class="add-to-list">here is my answer</div>

I have the id value below and tried this

 $('.add-to-list[data-action|="action-follow-global-id"][data-id|=id]').text('here is the things jtjt in the id value');

but no dice. Need to be AND'ing these together.

thx for help

like image 206
timpone Avatar asked Sep 02 '11 03:09

timpone


People also ask

How do I select an element by data-id?

Use the querySelector method to get an element by data attribute, e.g. document. querySelector('[data-id="box1"]') . The querySelector method returns the first element that matches the provided selector or null if no element matches the selector in the document.

How do you select element by id in jQuery?

The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.

How can get li id value in jQuery?

on('click', function (e) { var id = $(e. target). attr('id'); console. log(id); });


2 Answers

Didn't test it, but should work:

var id = 54;
$('.add-to-list[data-action=follow-global-id][data-id='+id+']').text('something');
like image 173
Dmitry F Avatar answered Sep 16 '22 15:09

Dmitry F


This will work:

$('.add-to-list[data-action="follow-global-id"][data-id="54"]').
    text('here is the things jtjt in the id value');

Here's a full code example you can execute to test:

<html>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.2.js"></script>
    <script>
    $(function(){
        $('.add-to-list[data-action="follow-global-id"][data-id="54"]').
            text('here is the things jtjt in the id value');
    });
    </script>
    <div data-id="54" data-action="follow-global-id" class="add-to-list">here is my answer</div> 
</html>
like image 25
Chris Pietschmann Avatar answered Sep 16 '22 15:09

Chris Pietschmann