views:

26376

answers:

5

Suppose I have a simple XHTML document that uses a custom namespace for attributes:

<html xmlns="..." custom:xmlns="http://www.example.com/ns"&gt;
    ...
    <div class="foo" custom:attr="bla"/>
    ...
</html>

How do I match each element that has a certain custom attribute using jQuery? Using

$("div[custom:attr]")

does not work. (Tried with Firefox only, so far.)

+1  A: 

look here http://pastebin.me/48d233d998b4b

basically its $('div').attr('custom:attr')

redsquare
I clarified my questin: I want to match each element that has a custom attribute, not get the value of the attribute.
Sebastian Rittau
+8  A: 

JQuery does not support custom namespaces directly, but you can find the divs you are looking for by using filter function.

// find all divs that have custom:attr
$('div').filter(function() { return $(this).attr('custom:attr'); }).each(function() {
  // matched a div with custom::attr
  $(this).html('I was found.');
});
Devon
I feared something like that. Thanks!
Sebastian Rittau
+6  A: 

This works in some conditions:

$("div[custom\\:attr]")

However, for a more advanced method, see this XML Namespace jQuery plug-in

Fyrd
The namespace plug-in is a dream.
pst
+1  A: 

the syntax for matching by attribute is:

$("div[customattr=bla]") matches --div customattr="bla"--

$("[customattr]") matches all tags with the attribute "customattr"

with namespace attributes like 'custom:attr' its not working

Here you can find a good overview: http://www.pamaya.com/jquery-selectors-and-attribute-selectors-reference-and-examples/

Suphi Basdemir
A: 

OK, so now of them works for me. I am trying to do the same thing, I have gone thru all the similar articles on stackoverflow n over the internet but it doesnt work for me.

I am able to access all the default attributes easily but I am not able to grab the custom attribute.

in the .ascx page I have elements that has a custom attribute:

<input id="update_title" type="text"  value="update title:"
                    data-helpid="3" style="width:270px;" />

and then in the javascript attached to the Master page I have

  $("[data-helpid]").each(function() {
    alert('test');   
});

I have tried many variabtions of it but it doesnt seem to work. What am I missing?

Hna0002