You must Sign In to post a response.
  • Category: JQuery

    How to get html tag based on attribute text in Jquery ?

    Hi Team.
    I have data like below format.


    <span id="1" dnd-draggable={value:'a',text:'apple'}/>
    <span id="2" dnd-draggable={value:'b',text:'bat'}/>

    here i want to get 'span' tag based on 'value' in dnd-draggable attribute.

    for example:

    if i give 'a', i will get first 'span' tag.




    Thanks
    Nanda Kishore.CH
  • #764347
    Hi Nanda Kishore,

    Please check this code snippet:

    Add this script inside <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    <script>
    function FindSpan()
    {
    var valueVariable = document.getElementById("DemoTextBox").value;
    $('span[dnd-draggable]').each(function ()
    {
    if ($(this).attr("dnd-draggable").indexOf("{value:\'" + valueVariable + "\',") != -1)
    {
    alert($(this).attr('id'));
    }
    });
    }
    </script>

    Add this controls inside <form>
    <asp:TextBox runat="server" ID="DemoTextBox" />
    <asp:Button runat="server" ID="demoButton" Text="Find" OnClientClick="javascript:return FindSpan();"/>
    <span id="1" dnd-draggable={value:'a',text:'apple'}/>
    <span id="2" dnd-draggable={value:'b',text:'bat'}/>

    Please enter a or b inside textbox and then on click of button, you will get the exact span id.

    Hope this will help you.
    Regards,
    Shashikant Gurav[Programmer II]
    shashikantgurav22@gmail.com


  • Sign In to post your comments