简体   繁体   中英

How to Get particular child node class name using javascript?

I want to get particular child node(ie class name). how can i do this. This is my code

    <div id="mess">
                     <h1>Bhashyam School</h1>
                        <p>AC Nagar Main Road, Nellore <br/>Ho, Nellore - 524001  </p>
                     <p class="tel">Telephone: +(91)-9603444376 </p>
                     <p class="email">Email: <a href="mailto:info@citydeal.in">info@citydeal.in</a></p>
                     <p class="web">Website: <a href="http//citydeal.in" target="new">http//citydeal.in</a></p>
                     <p class="description">Hours of Operation : 
                        Monday, Tuesday, Wednesday, Thursday, Friday, Saturday: 09:30 am to 10:00 pm<br>
                        Sunday: Closed , Modes of Payment : 
                        Cash
                     </p>

<form name="contactform" method="post" action="send.php" onsubmit="return getChilds();">
                    <p>
                        <input id="box" type="text" name="telephone" placeholder="Enter Mobile Number"/>

                        <input type="hidden" name="strToSend" id="strToSend"/>
                        <button type="submit" class="button" value="Submit">Get SMS</button>
                    </p>
                </form>
    </div>

    <script type="text/javascript">
        function getChilds()
    {
        var arr=document.getElementById('mess').childNodes, str='';
        for(var i=0; i<arr.length; i++) {
            if(arr[i].innerHTML!=undefined) {
                str+=" "+(arr[i].textContent||arr[i].innerText);
            }
        }
        document.getElementById("strToSend").value=str;
        return true;
    }
    </script>

In the above javascript code i want childNodes with particular class names (ex:p class="tel", p class="email").

With jQuery ?

.children(".yourClass")

Documentation

And as I said in comments , without jQuery :

.querySelectorAll(".yourClass")

Documentation

And because I'm the nicest person in the world, here is a working JSFiddle with .querySelectorAll() :

http://jsfiddle.net/F8DeQ/

(In order for you to see the result, I set your hidden input to text )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM