I found this link very useful
get selected textbox id jQuery
however, what i wanted to do now is to reuse this id to do some trimming function for each textbox where it is actually looping. I have tried this code:
<html>
<head>
<?php echo $this->Html->script("jquery-1.4.3.min"); ?>
</head>
<body>
<?php
echo $content_for_layout;
?>
<script type="text/javascript">
$(document).ready(function () {
// alert('JQuery is succesfully included');
/* $(':submit').click(function(){
<?php if(($this->request->controller=='members')&&($this->action=='register')):?>
//alert('well');
var some = $.trim($(":text").val());
alert(some);
<?php endif; ?>
});
*/
$(':submit').click(function(){
$('form input[type="text"]').each(function(){
//get value of id of textbox here
var id = $(this).attr('id');
var some = $.trim($((this).attr('id')).val());
alert(some);
});
});
});
</script>
</body>
</html>
yet it doesnt pops out the alert box.
I’m a bit confused about exactly what you are trying to achieve, but there is a problem on this line:
Specifically,
(this).attr('id')doesn’t work because(this)is interpreted asthisandthisis the current DOM element which doesn’t have an.attr()method.I think you probably wanted to do this:
That is, you are trying to get the id from the current element then use that id to select the element and get its value, then trim the value. But you don’t need the id to get the value given you already have a reference to the element – you can just do this:
If you want to loop through all the textboxes and set them to the trimmed version of their current values you can do this:
Or:
See the
.val()doco for information about passing a callback function to.val().